未知的课程? C2143语法错误:缺少“;”在'*'之前

时间:2014-03-12 12:28:29

标签: c++ syntax syntax-error rhapsody

我收到错误“C2143:语法错误:缺少';'在Track.h中的'*'之前 我认为这是由于“缺失”的类定义。

这是3个头文件:

Topics.h,包级头文件,#包含其他所有内容:

#ifndef Topics_H
#define Topics_H

#include <oxf\oxf.h>
#include "Request.h"
#include "TDPoint.h"
#include "Track.h"
#include "TrackReport.h"

#endif

然后是TDPoint(如“3DPoint”中),它只是定义了一个具有3个长属性的类:

#ifndef TDPoint_H
#define TDPoint_H

#include <oxf\oxf.h> // Just IBM Rational Rhapsody's Framework
#include "Topics.h"

class TDPoint {
    ////    Constructors and destructors    ////

public :

    TDPoint();

    ~TDPoint();

    ////    Additional operations    ////

long getX() const;    
void setX(long p_x);
long getY() const;    
void setY(long p_y);    
long getZ() const;
void setZ(long p_z);

    ////    Attributes    ////

protected :

    long x;    
    long y;    
    long z;};

#endif

但问题出在这里,标记为:

#ifndef Track_H
#define Track_H

#include <oxf\oxf.h> // Just IBM Rational Rhapsody's Framework
#include "Topics.h"
#include "TDPoint.h"

class Track {

public :

    ////    Operations     ////

    std::string getId() const;

    void setId(std::string p_id);

    TDPoint* getPosition() const; // <--- This line, the first line to use TDPoint, throws the error

    ////    Attributes    ////

protected :

    std::string id;   

    TDPoint position;

public :

     Track();
     ~Track();
};

#endif

我的猜测是编译器(MS VS2008 / MSVC9)根本不知道“TDPoint”类。但是,即使在与“Track”相同的头文件中定义类,或者使用像“TDPoint”这样的前向声明(然后抛出错误:未定义的类)也无济于事。 代码是从Rhapsody自动生成的,如果这有任何区别的话。

但也许错误完全是另一回事?

3 个答案:

答案 0 :(得分:7)

Topics.h包括TDPoint.hTrack.h

TDPoint.h包括Topics.h

Track.h同时包含Topics.hTDPoint.h

这感觉就像一个循环包含......您应该转发声明您的类来解决它或修改Topics.h以使其不具有循环性。

答案 1 :(得分:3)

您有循环包含:文件Track.h包括Topics.h,其中包含TDPoints.h,其中包含Topics.h,其中包含Track.h TDPoint类没有宣布。

事实上,TDPoint.h根本不需要任何头文件,它完全独立(根据你问题中显示的代码)。

Track.h文件只需要包含TDPoint.h,而不是Topics.h。 (可能<string>。)

常规提示:在头文件中包含尽可能少的标题。

答案 2 :(得分:0)

其他答案是正确的,但是我想补充一些内容以保持完整性。

1。原因:,您的项目具有循环性,尤其是当您编译“ TDPoint.cpp”时,编译器将执行以下操作

#include "TDPoint.h" //start compiling TDPoint.h
#include "Topics.h" //start compiling Topics.h
#include "TDPoint.h" //compilation of TDPoint.h skipped because it's guarded
#include "Track.h" //start compiling Track.h
#include "Topics.h" //compilation of Topics.h skipped because it's guarded
 //resume compiling Track.h 
... 
TDPoint* getPosition() const; //=> error TDPoint is not defined

=>C2143: syntax error: missing ';' before '*' 

2。对策:用前向声明替换标头中的包含,以删除包含的圆圈,并在.cpp文件中使用包含。具体来说,前向声明意味着: (在Topics.h中)

#ifndef Topics_H
#define Topics_H
#include <oxf\oxf.h>
#include "Request.h"
class TDPoint;  //Forward declaration to replace #include "TDPoint.h"
class Track; //Forward declaration to replace #include "Track.h"
#include "TrackReport.h"
#endif
相关问题