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.
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.