I'm trying to get some framework off the ground for a project involving drawing a game board using 2D vectors. This is an early framework....All I want is to get something to compile. Let's just say for now I want a 5 X 5 matrix. I can't quite get the call part to work so it will compile. Can someone who is good with C++ look at this code and tell me whats wrong. Thanks.
#include%26lt;iostream%26gt;
#include%26lt;vector%26gt;
using namespace std;
class georgeClass
{
private:
vector%26lt;vector%26lt;int%26gt; %26gt; myVec;//sets up multi D vector
public:
georgeClass(int);
void printRow(int);
};
georgeClass::georgeClass(int x)
{
vector %26lt;int%26gt; row(x,0);
for(int i=0;i%26lt;x;i++)
myVec.push_back(row);
for(int j=0;j%26lt;x;j++)
myVec[i][j]=i+j;
}
void georgeClass::printRow(int r)
{
for(int i=0;i%26lt;myVec[r].size();i++)
cout%26lt;%26lt;myVec[r][i]%26lt;%26lt;endl;//I don't what it fills up with for now
}
int main()
{int x=5;// should tell constructor to make a 5X5 matrix
georgeClass vec(x);
int r;
cout%26lt;%26lt; vec.printRow(r);//%26lt;-----something wrong here???????????????
}
C++ building a class with 2 dimensional vectors?
The problem is PrintRow() returns void. You're trying to put void into cout, and that object definitely won't have overloaded the %26lt;%26lt; operator to take void.
Your PrintRow() functions prints out the row internally. Change "cout%26lt;%26lt; vec.printRow(r);" to "vec.printRow(r);".
Hope this helps. I haven't checked for any other errors, so it may still fail to compile.
Reply:First, without the errors it's hard to tell what's wrong. Why don't you use a 2D array instead.. or use a single Vector thats (height * width) long, and index the item you want by using J*width+i or similar.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment