Using an STL List of a Struct in C++
I know I'm probably doing something unforgivably stupid here, but for some reason my code won't compile and I'm not sure why.
#include <iostream>
#include <list>
//A queue for the working set
//x,y co-ords of the square, path length so far
struct square {
int x;
int y;
int path_length;
} square;
list<square> workingset;
I have other code which appears 开发者_如何学JAVAto create a list in exactly the same way -
#include <iostream>
#include <list> //List class library
#include <algorithm> //STL algorithms class library (find)
using namespace std;
list<int> numberlist; //Creates my list
And the problem doesn't appear to be because of the struct, as I have tried making a list of ints too, and it won't work either.
The errors I am getting are -
syntax error : missing ';' before '<' and missing type specifier - int assumed.
(Both on the line in which I am trying to declare a list)
So what incredibly stupid thing am I missing here? :3
The list
class is defined in the std
namespace, so you have to either do this:
std::list<square> workingset;
Or this
using namespace std;
list<square> workingset;
Put std::
in front of the list<square> workingset;
you could just include using namespace std;
, but it's not that good idea, if this is in a header file.
Also, you can't use square
as the name of the struct as well as the name of a variable.
The following code should fail to compile:
struct square {
int x;
int y;
int path_length;
} square;
square getSquare
{
return square();
}
Try this instead:
struct square {
int x;
int y;
int path_length;
} aSquare;
square getSquare
{
return square();
}
精彩评论