I am trying to initialize a deque with pointers to a user defined struct, Tile, in order to eliminate unnecessary copying.
My code looks like this:
Tile *start = new Tile(0,0,0, 'q', nullptr);
deque<Tile*> search();
search.push_front(start);
The above code is located in main.cpp.
The Tile struct looks like this, and is contained in hunt.h:
struct Tile
{
int row; int col; int farm;
char tile;
Tile * added_me;
Tile(int f, int r, int c, char t, Tile * a) :
farm(f), row(r), col(c), tile(t), added_me(a){}
};
The layout of my program is as follows:
main.cpp: includes "io.h"
io.h: includes "hunt.h", various standard libraries
hunt.h: includes vector, deque, Tile struct
However, I am getting an error in main.cpp when I try to push_front(start): expression must have class type." I wasn't sure if a possible fault in my #includes was causing this error, so please let me know if this is the case. Otherwise, I am not entirely sure how to fix this error.
Thanks in advance!
When you write
deque<Tile*> search();
you aren't actually declaring a deque<Tile*>
named search
and using the default constructor. Instead, C++ interprets this as a function declaration for a function named search
that takes no parameters and returns a deque<Tile*>
. You can't call push_front
on a function, hence the error.
To fix this, either remove the ()
from the declaration of the variable, or replace them with {}
(if you're using a C++11-compliant compiler). That will cause C++ to (correctly) interpret that you want to declare a variable.
Hope this helps!