Eilidh
Eilidh

Reputation: 1374

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 to 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

Upvotes: 0

Views: 3920

Answers (3)

R Sahu
R Sahu

Reputation: 206577

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();
}

Upvotes: 0

Rafid
Rafid

Reputation: 20189

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;

Upvotes: 3

Kiril Kirov
Kiril Kirov

Reputation: 38163

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.

Upvotes: 2

Related Questions