简体   繁体   English

C ++矢量矢量

[英]C++ Vector of vectors

I have a class header file called Grid.h that contains the following 2 private data object: 我有一个名为Grid.h的类头文件,其中包含以下2个私有数据对象:

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

And a public method whose prototype in Grid.h is such: 一个公共方法,其原型在Grid.h中是这样的:

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

The definition of above mentioned function is defined as such in Grid.cpp: 上面提到的函数的定义在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]; 在行return row[row][col]; the first row is the int& , not the vector . 第一rowint& ,而不是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. 在内部作用域中声明的变量是在外部作用域中隐藏变量,因此编译器尝试索引int而不是vector ,这显然是不能做的。

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. 编辑:此外,虽然你得到的错误表明编译器正在找到错误的row变量,正如A. Levy指出的那样,你的vector声明也有问题,所以即使你修改了变量名,如果您确实声明了此处所示的vector ,则无法编译。 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 . 另外,当您在头文件中执行此操作时,您将需要在std命名空间的前面添加std::标记 - 例如vector If it were in a cpp file, then you could use using namespace std; 如果它在cpp文件中,那么你可以使用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 . 如果没有std:: tag或using语句,编译器将无法识别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. C ++编译器很难说明嵌套模板类型和正确的位移运算符之间的区别。

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;
}

这就是你需要的:

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. 在我看来(虽然您可能需要自己验证这一点,我不想编写测试应用程序)问题来自于您的参数包含命名row并且您的类具有内部变量的事实row和命名冲突。

You may need to qualify which row you're using. 您可能需要限定您正在使用的行。 Consider: 考虑:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM