Compiler refuses to believe that I pass a type to the vector template

I am writing a text-based minesweeper. My idea is to implement the grid using vector of a vector, to get the two-dimensional indexing to work. The header looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13

class Grid
{
    private:
        int width;
        int height;
        int TotalMine;      // total number of mines

        vector < vector<int> > mine;         // mine distribution and number
                                             // -1 if mine, otherwise number
                                             // of neighboring mines
...



I am using Code:Blocks with mingw on 64-bit Win7. Now, strangely enough, when I compile the entire project, the compiler always returns

 
error: ISO c++ forbids declaration of 'vector' with no type


for that line with vector declaration.



I wrote a separate test program, to which I

1) copy-pasted this part of the code;
2) added a constructor which set all elements to zero and a getter for the elements;
3) had the main() to just call the constructor and then print a couple of the elements.

And it compiled with no error!



Comments and suggestions, please? I am nearly frustrated enough to smash my laptop now. Guess I should walk away and have a glass of water...
Are you using namespace std;? If not, make sure you declare your variable as std::vector<std::vector<int>> mine;

Edit: Aside from that, you don't happen to be using MSVC++ do you?
Last edited on
Hmm.

Vector<A> B;

but with one inside..

Vector <Vector<A2>B1>B
...

Maybe your error is because you forgot the 'B1' as it is in my code.

It is like saying int; with nothing after it. Try that. I don't know if that is it or not.
Maybe your error is because you forgot the 'B1' as it is in my code.


No, you don't give names to variables that are template types. Otherwise you would also have to do:

std::vector<A a> B;//or somesuch
No, you don't need a variable name. You are declaring a vector of vectors or ints. Or in more basic terms, b is a vector, that vector will contain vectors, which will contain ints.

You can nest vectors fairly deep, but typically anymore than 2 vectors means you should rethink your strategy.
Volatile Pulse:

That was the problem! Thanks so much!
I had std in my main.cpp, but not in any of those the headers.



On a related note, if not nested vectors, how would you people then handle an n-dimensional array-like object in a class? The straightforward

 
int grid[d1][d2]...[dn]


doesn't seem to sit well with the writing of a class constructor.

Theoretical physics is my day job, and we do deal with n-dim spaces with generic n all the time. Anything that doesn't structurally reflect the n-dim nature should be seen as a kludge, shouldn't it?
n-dimensional arrays aren't typical, specifically because n-dimensions require resizing. As for vectors, it's not going to accomplish what you want. Each element in the base vector can have subsequent vectors of different sizes.

mine[0] could have a vector of 2
mine[1] could have a vector of 10
mine[2] could have a vector of 0

The vectors don't resize them selves because another one of a larger size is added.
Topic archived. No new replies allowed.