tags:

views:

148

answers:

6

I'm trying to use list in c++, but I get the following error:

1>error C2143: syntax error : missing ';' before '<'

1>error C4430: missing type specifier int assumed. Note: C++ does not support default-int

1>error C2238: unexpected token(s) preceding ';'

With the following code:

#pragma once

#include "Includes.h"

class Polygon
{
public:
    Polygon(void);
    ~Polygon(void);

    void addVertice(hgeVector v);
    void renderPolygon();
    list<hgeVector> vertices;
};

Includes.h:

#ifndef INCLUDES
#define INCLUDES

#define safe_delete(d) if(d) { delete d; d=0; }
#define PI 3.14159
#include <stdio.h>
#include <list>
#include "\include\hge.h"
#include "\include\hgesprite.h"
#include "\include\hgefont.h"
#include "\include\hgeparticle.h"
#include "\include\hgerect.h"
#include "Car.h"
#include "HelperFunctions.h"
#include "config.h"
#include "Polygon.h"

using namespace std;

#endif
+1  A: 

class template need a full type declaration to instantiate itself. Make sure you have included the header file where hgeVector is declared.

BTW, you have 'using namespace std‘ in your header - this is not a good practice. It will introduce unnecessary names to the current namespace.

Findekano
It's _class template_, not template class (because it's a template from which classes are instantiated, not the other way around).
sbi
Thanks - it is always difficult to figure out the subtle English terms for a non-native English speaker. I have fixed it.
Findekano
+1 for mentioning that `using namespace std;` in headers is bad practice.
quamrana
+4  A: 

I think you have circular "includes". You are including Includes.h in Polygon.h and including Polygon.h in Includes.h.

AraK
If there is a guardance marco, or 'pragma once', this is not a problem, right?
Findekano
That might well be the problem. `#pragma once` should protect against this, but perhaps it isn't.
Philip Potter
The problem *is* the include guards. Think about exactly what happens with respect to the `using namespace std` line and the `list<hgeVector>` line when a third file writes `#include <includes.h>`
Tyler McHenry
should I include "polygon.h" in polygon.cpp and include "Includes.h" in polygon.h?EDIT: It did not solve the problem.
Per
OP should be using MSVC, which supports `#pragma once`.
KennyTM
Yes, and you should not include `polygon.h` (or any other file with an `#include <includes.h>` directive) in `includes.h`.
Tyler McHenry
@Tyler Why? I'm using the includes.h in all of my files.
Per
The problem you are having is stemming from circular inclusion (where two files each include each other). Read my answer to this question that explains why you reach the `list<hgVector>` line before the `using namespace std` line when a third file includes `includes.h`. In general, circular inclusion is a bad thing because it causes confusing issues like this.
Tyler McHenry
@AraK: Good catch. I thought this smelled of circular includes, but I thought it was too little information and didn't even bother looking close. You had a very good eye there... +1
sbi
+6  A: 

The issue could be that the line list<hgeVector> vertices is being processed before using namespace std;, and so your compiler does not know what a list (without the std:: namespace qualifier) is. It's not clear to me in exactly what order these statements get processed since your two files include each other, and I don't know precisely how the non-standard #pragma once will handle this.

In any case, try qualifying list<hgeVector> as std::list<hgeVector>

Edit: Assuming #pragma once works just like include guards, then this problem will occur if some other file inlcudes includes.h, but not if some other file includes Polygon.h. If another file includes includes.h, what happens is that includes.h reaches #include <Polygon.h>, and the compiler starts processing Polygon.h. But then when #include <includes.h> is reached inside Polygon.h, nothing is effectively included since the INCLUDES guard is already defined, so you don't get the using namespace std; line before the compiler continues processing the rest of Polygons.h.

In general, try to avoid circular inclusion, and prefer forward-declarations.

Tyler McHenry
Did you mean "forward-declarations"?
Roger Pate
Yes, I certainly did.
Tyler McHenry
Thanks alot!It worked after putting std:: before list<hgeVector>
Per
A: 

Make sure hgeVector is defined.

You may have redefined list somewhere. Try using std::list.

Try something very simple like std::list<int>.

Zan Lynx
+6  A: 

Just some general comments...

 #define PI 3.14159

Please use M_PI in math.h, which is 3.141592653589793238462643.

#include "\include\hge.h"
#include "\include\hgesprite.h"
#include "\include\hgefont.h"
#include "\include\hgeparticle.h"
#include "\include\hgerect.h"

You should use forward slashes / here, and remove the leading \ before the include.

using namespace std;

Avoid this in header file. This will pollute all other users' global namespace. (Therefore, you should use std::list<hgeVector> vertices; in Polygon.h.)

KennyTM
A: 

The answer (as Tyler McHenry pointed out) was circular inclusion!

After sorting out my includes I ended up with a compiled code like this (even without std:: infront of list:

#pragma once

#include <list>
#include "D:\Programmering\haffes\hge181\include\hge.h"
#include "D:\Programmering\haffes\hge181\include\hgevector.h"
using namespace std;

using namespace std;

class MyPolygon
{
public:
    MyPolygon(void);
    ~MyPolygon(void);

    void addVertice(hgeVector v);
    void renderPolygon();
    void setHotSpot(hgeVector v);
    void translate(hgeVector v);
private:
    list<hgeVector> vertices;
    hgeVector hotSpot;
    bool hotSpotUndef;
};

Thanks a bunch for all the fast and good answers!

Per
Avoiding stuff like `#include "D:\Programmering\haffes\hge181\include\hge.h"` is why compilers support using `-I directory` to add it to the include path. Then the code compiles even if it's located somewhere else.
bk1e