Gameboard* m_Board = new Gameboard(x,y)[100];
Constructor With Array
I'm trying to allocate space for an array of a class that accept two arguments (x and y) in its constructor. This is my code, for some reason i keep getting an error. If I just remove the array part it compiles just fine.
You can't do that!
Try
Gameboard* m_Board[100]; // ( for stack variable)
Gameboard** m_Board = new Gameboard*[100]; // ( for heap variable)
for (int i = 0; i < 100; ++i)
{
m_Board = new Gameboard(x,y);
}
Try
Gameboard* m_Board[100]; // ( for stack variable)
Gameboard** m_Board = new Gameboard*[100]; // ( for heap variable)
for (int i = 0; i < 100; ++i)
{
m_Board = new Gameboard(x,y);
}
You need to have a default (i.e. no argument or all default arguments) contructor if you use operator new[] for an array of elements.
Don't ask me why...
Don't ask me why...
Quote:Original post by j0mich01
I'm trying to allocate space for an array of a class that accept two arguments (x and y) in its constructor.
Array elements can only be default-constructed.
Of course, if you were to use the standard library you could get the behaviour you desire without incurring loss of locality-of-reference and significant additional memory management issues:
std::vector< Gameboard > m_board(100, Gameboard(x, y));
constructs a vector containing 100 copies of the gives Gameboard.
Σnigma
std::vector< Gameboard > m_board(100, Gameboard(x, y));
constructs a vector containing 100 copies of the gives Gameboard.
Σnigma
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement