I'm currently teaching myself C++ and have been working on a project using ncurses and vectors to create a simple maze game. I have been doing everything bit by bit, always writing a section of code, then compiling and running to see if everything is working. I've been trying to use a vector push_back which I had planned to use to create the walls of my maze and potentially replace the addch commend I used to establish the boarders. That is the point where things beak down. At this point, everything is a work in progress and there are a few variabls which have ot been used yet. The code is below and I have add a comment where the code isn't compiling.
//This is the creation of the board.Size if fixed. Need to look at how I can maximize this.
while((Esc=getch())!=27){
for(i=0; i<maxX-1; i++)
{
move(0,i);
addch(Wall);
}
for(i=0; i<maxX-1; i++)
{
move(maxY-2,i);
addch(Wall);
}
//Everything works well until I get to this part of the code. at this point I get an "error: expected initializer before ‘<’ token" Message
MazeClass MazeClass::vector<MazeWall> Maze.push_back(Wall(40, 50));
refresh();
}
return 0;
}
The error message is "75:29: error: expected initializer before ‘<’ token"
Would someone be able to help me out here a bit. I've been looking through the internet for the last 3 hours and can't figure out what I'm doing wrong.
Thnks for the help. That seems to have corrected the issue of the initializer. I've changed the code as follows:
MazeClass
std::vector<MazeWall> MazeClass::Maze{40,50};
I was getting an error message for the double brackets, but after removing them, the error went away. Unfortunately, I'm now getting a:
"76:23: error: invalid declarator before ‘MazeClass’
std::vector<MazeWall> MazeClass::Maze{40,50};"
^
I've played arouwith the command a bit and did a bit more net research, Most f the problems I've found seem to revolve around a missing semicolon, which as far as I can tell is not the case here.
Last annoying question as well, do I understand you correctly that I will not be able to use the push_back function at all, or is it just not an option because of how I have organized the code. In the case of the latter, is there a simplefix for that, or do I need a significant rewrite of my code?
Again, thank you very much for the input.
I'm sorry. I thought it looked like an attempt to initialize a static member variable but I see now that Maze is just a regular non-static member variable so you might want to ignore what I said earlier.
If Maze is a non-static member variable (as it is in your code) you need a MazeClass object in order access the Maze vector. Also note that Maze is private so you don't have direct access to it from outside the class.
1 2 3
MazeClass maze;
maze.Maze.push_back(Wall(40, 50)); // error, MazeClass::Maze is private
maze.someMemberFunctionThatUsesMaze(); // OK, assuming such a function exists
I've tried to use your example. I've change the code for the class as follows:
class MazeClass{
public:
int x,y,maxX,maxY;
char Wall;
maze;
MazeWall MazeW;
std::vector<MazeWall> Maze;
MazeClass();
~MazeClass();
and then following that, I amended the vector code later to read
MazeClass maze;
maze.Maze.push_back(Wall(40, 50));
The initiation error is now gone, but for a reason I don't understand It has a problem with the vector and is giving me this error:
18:1: error: ‘maze’ does not name a type
maze; //This seems to be a problem in how I tried to declare maze in the class
^
81:32: error: ‘Wall’ cannot be used as a function
maze.Maze.push_back(Wall(40, 50)); // Here is a problem with the actual vecor code at the end.
I'm guessing I have declared maze incorrectly in the class, and that is causing the other problems. AM I at least on the right track?