开发者

Compile errors when trying to use list in C++

开发者 https://www.devze.com 2022-12-22 16:55 出处:网络
I\'m trying to use list in c++, but I get the following error: 1>error C2143: syntax error : missing \';\' before \'<\'

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 in开发者_JAVA百科t 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


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.)


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.


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


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.


Make sure hgeVector is defined.

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

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


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号