C++ Vector of vectors

后端 未结 5 779
不思量自难忘°
不思量自难忘° 2021-02-06 01:56

I have a class header file called Grid.h that contains the following 2 private data object:

vector column;
vector> row;
         


        
5条回答
  •  深忆病人
    2021-02-06 02:54

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

    or

    std::vector< std::vector > 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.

提交回复
热议问题