0

I am currently attempting to create a class containing a 2D vector of structs, however I am clearly doing something tragically wrong as whenever I run my code, I get the error at line 19 (the line in the for loop in the world constructor) error: expected primary-expression before ‘)’ token

Here is my code at the moment:

#include <iostream>
#include <vector>
struct Cell{
    bool isAlive;
    Cell(bool alive){
        isAlive = alive;
    }
};
class World{
    unsigned width;
    unsigned height;
    std::vector<std::vector<Cell>> state;

public:
    World(unsigned w,unsigned h){
        width = w;
        height = h;
        for (int i = 0; i<h; i++){
            state.push_back(std::vector<Cell>);
        }


    }
};

I know that I haven't yet fully initialised the 2nd dimension of the vector, but I'm just trying to get this working at the moment.

Thank you so much, I honestly have no clue what this error means.

CyberJelly
  • 25
  • 2
  • Does this answer your question? [Initializing a two dimensional std::vector](https://stackoverflow.com/questions/17663186/initializing-a-two-dimensional-stdvector) – bloody Dec 09 '20 at 21:37

2 Answers2

0

You need to construct the vectors you are attempting to push_back.

// Note the added parentheses after <Cell>
state.push_back(std::vector<Cell>())

In this case, push_back is expecting a value, or an "expression" which evaluates to a value. Your expected primary expression before ')' token error more or less means that push_back was expecting a value as its parameter but was passed something else. In this case, that something else was the type std::vector<Cell>. Constructing the vector (creating a value with an expression) will fix the issue.

scupit
  • 704
  • 6
  • 6
0

Ever heard of "there is no spoon"? In this case, there is no vector. You cannot pass a type, you have to pass an instance or value in this case.