尝试在C++中使用列表时编译错误

0 c++ list

我正在尝试在c ++中使用list,但是我收到以下错误:

1>错误C2143:语法错误:缺少';' 在'<'之前

1>错误C4430:假定缺少类型说明符int.注意:C++不支持default-int

1>错误C2238:';'之前的意外令牌

使用以下代码:

#pragma once

#include "Includes.h"

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

    void addVertice(hgeVector v);
    void renderPolygon();
    list<hgeVector> vertices;
};
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

ken*_*ytm 7

只是一些一般性意见......

 #define PI 3.14159
Run Code Online (Sandbox Code Playgroud)

请使用M_PImath.h,这是3.141592653589793238462643.

#include "\include\hge.h"
#include "\include\hgesprite.h"
#include "\include\hgefont.h"
#include "\include\hgeparticle.h"
#include "\include\hgerect.h"
Run Code Online (Sandbox Code Playgroud)

你应该/在这里使用正斜杠,并在\之前删除前导include.

using namespace std;
Run Code Online (Sandbox Code Playgroud)

在头文件中避免这种情况.这将污染所有其他用户的全局命名空间.(因此,你应该使用std::list<hgeVector> vertices;Polygon.h).


Tyl*_*nry 5

问题可能是list<hgeVector> vertices之前正在处理该行using namespace std;,因此您的编译器不知道list(没有std::命名空间限定符)是什么.由于你的两个文件相互包含,我不清楚这些语句的确切顺序是什么,我不知道非标准#pragma once将如何处理这个.

无论如何,请尝试排位list<hgeVector>std::list<hgeVector>

编辑:假设#pragma once工作就像包含警卫一样,如果某些其他文件包含h,则会出现此问题,但如果某些其他文件包含Polygon.h则不会出现此问题.如果另一个文件包含includes.h,那么include.h会发生什么#include <Polygon.h>,编译器开始处理Polygon.h.但是当#include <includes.h>在Polygon.h中到达时,由于INCLUDES已经定义了防护,所以没有任何有效的包含,所以using namespace std;在编译器继续处理Polygons.h的其余部分之前,你没有得到该行.

一般来说,尽量避免循环包含,并且更喜欢前向声明.