【问题标题】:Compile errors when trying to use list in C++尝试在 C++ 中使用列表时编译错误
【发布时间】:2011-01-24 16:50:33
【问题描述】:

我正在尝试在 c++ 中使用列表,但出现以下错误:

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

包括.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

【问题讨论】:

  • 对于它的价值,safe_delete 是一个糟糕的主意。它会掩盖代码中的逻辑问题。请改用智能指针(例如boost::scoped_ptrboost::shared_ptrboost::scoped_arrayboost::shared_array)。
  • 为什么还要麻烦安全删除中的if(d)
  • @Per safe_delete 其实没用,删除空指针是安全的!
  • 更不用说在使用safe_delete的给定实现时可能出现问题的所有标准“宏是邪恶的”方式:parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.4
  • 重新安全删除,Stroustrup 建议这样做:template&lt;class T&gt; inline void destroy(T*&amp; p) { delete p; p = 0; }www2.research.att.com/~bs/bs_faq2.html#delete-zero

标签: c++ list


【解决方案1】:

只是一些一般的cmets...

 #define PI 3.14159

请在math.h 中使用M_PI,即3.141592653589793238462643。

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

您应该在此处使用正斜杠/,并删除include 之前的前导\

using namespace std;

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

【讨论】:

    【解决方案2】:

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

    无论如何,请尝试将list&lt;hgeVector&gt; 限定为std::list&lt;hgeVector&gt;

    编辑:假设#pragma once 就像包含保护一样工作,那么如果其他文件包含includes.h,则会出现此问题,但如果其他文件包含Polygon.h,则不会出现此问题。如果另一个文件包含includes.h,那么includes.h 会到达#include &lt;Polygon.h&gt;,编译器开始处理Polygon.h。但是,当在 Polygon.h 中到达 #include &lt;includes.h&gt; 时,由于已经定义了 INCLUDES 保护,因此实际上没有包含任何内容,因此在编译器继续处理 Polygons.h 的其余部分之前,您不会得到 using namespace std; 行。

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

    【讨论】:

    • 您的意思是“前向声明”吗?
    • 非常感谢!将 std:: 放在 list 之前后它起作用了
    【解决方案3】:

    我认为您有循环“包含”。您在Polygon.h 中包含Includes.h,在Includes.h 中包含Polygon.h

    【讨论】:

    • 如果有一个guardance marco,或者'pragma once',这不是问题,对吧?
    • 这很可能是问题所在。 #pragma once 应该防止这种情况发生,但也许不是。
    • 问题包含守卫。想想当第三个文件写入#include &lt;includes.h&gt;时,using namespace std 行和list&lt;hgeVector&gt; 行会发生什么
    • 我应该在polygon.cpp 中包含“polygon.h”并在polygon.h 中包含“Includes.h”吗?编辑:它没有解决问题。
    • OP应该使用MSVC,支持#pragma once
    【解决方案4】:

    类模板需要一个完整的类型声明来实例化自己。确保已包含声明 hgeVector 的头文件。

    顺便说一句,您的标头中有“using namespace std”——这不是一个好习惯。它将为当前命名空间引入不必要的名称。

    【讨论】:

    • 它是 类模板,而不是模板类(因为它是从中实例化类的模板,而不是相反)。
    • 谢谢 - 对于非英语为母语的人来说,总是很难弄清楚微妙的英语术语。我已经修好了。
    • +1 提到标题中的using namespace std; 是不好的做法。
    【解决方案5】:

    确保已定义 hgeVector

    您可能在某处重新定义了列表。尝试使用std::list

    试试std::list&lt;int&gt;这样非常简单的东西。

    【讨论】:

      【解决方案6】:

      答案(正如 Tyler McHenry 指出的)是循环包含!

      整理完我的包含后,我最终得到了这样的编译代码(即使没有 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;
      };
      

      非常感谢所有快速而好的答案!

      【讨论】:

      • 避免像#include "D:\Programmering\haffes\hge181\include\hge.h" 这样的东西是编译器支持使用-I directory 将其添加到包含路径的原因。然后代码编译,即使它位于其他地方。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-20
      • 2020-11-13
      • 2021-06-17
      • 1970-01-01
      • 1970-01-01
      • 2022-10-02
      相关资源
      最近更新 更多