开发者

C++ Vector of vectors

开发者 https://www.devze.com 2022-12-28 01:27 出处:网络
I have a class header file called Grid.h that conta开发者_StackOverflow中文版ins the following 2 private data object:

I have a class header file called Grid.h that conta开发者_StackOverflow中文版ins the following 2 private data object:

vector<int> column;
vector<vector<int>> row;

And a public method whose prototype in Grid.h is such:

int getElement (unsigned int& col, unsigned int& row);

The definition of above mentioned function is defined as such in Grid.cpp:

int getElement (unsigned int& col, unsigned int& row)
{
    return row[row][col] ;
}

When I run the program, I get this error:

error C2109: subscript requires array or pointer type

Whats going wrong?


In the line return row[row][col]; the first row is the int&, not the vector.

The variable declared in the inner scope is shadowing the variable in the outer scope, so the compiler is trying to index an int rather than a vector, which it obviously can't do.

You should fix your variable names so that they don't conflict.

EDIT: Also, while the error that you're getting indicates that the compiler is finding the wrong row variable, as A. Levy points out, you also have a problem with the declaration of your vector, so even if you fix the variable names, if you have indeed declared the vector as shown here, it won't compile. Nested templates need spaces between the > symbols, otherwise the compiler will read >> as a right-shift operator rather than part of a template declaration. It needs to be

std::vector<std::vector<int> > row;

or

std::vector< std::vector<int> > row;

In addition, as you're doing this in a header file, you're going to need to tack the std:: tag on the front of anything from the std namespace - such as vector. If it were in a cpp file, then you could use using namespace std; but that would be very bad to do in a header file (since it would pollute the global namespace). Without the std:: tag or the using statement, the compiler won't recognize vector.


This is probably not the index problem, but you also need a space between the nested angle brackets in your vector of vectors type declaration. C++ compilers have a hard time telling the difference between nested template types and the right bit shift operator.

Example:

vector<vector<int> >  vec2d;        // Good.

vector<vector<int>>   anotherVec2d; // Bad!

vector< vector<int> > yetAgain;     // Best IMHO. 
                                    // Keeps the white space balanced.


I think you want something like this... (although I cannot imagine why :-))

#include <vector>
#include <iostream>

using namespace std;

typedef vector<int> row;
typedef vector<row> matrix;

matrix mat(2,2);

int getElement (unsigned int ri, unsigned int ci)
{
    return mat[ri][ci] ;
}

int main() {

    mat[1][0] = 1234;
    cout << getElement(1,0) << endl;

    return 0;
}


This is what you need:

return Grid::row[row][col];


It appears to me (although you may need to verify this on your own, I don't feel like writing up a test application) that the problem is coming from the fact that your parameter contains a named row and your class has an inner variable row and there is a naming conflict.

You may need to qualify which row you're using. Consider:

return Grid::row[row][col];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号