2014-03-12 75 views
3

我收到錯誤「C2143:syntax error:missing';'之前,「*」在Track.h 我相信這是由於「失蹤」類定義未知課程? C2143語法錯誤:缺少「;」在'*'之前

這些都是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。」但即使是定義在相同的頭文件爲「類跟蹤「,或者使用像」class TDPoint「這樣的前向聲明(然後拋出錯誤:未定義的類)沒有幫助。 該代碼是從Rhapsody自動生成的,如果這有什麼區別的話。

但也許錯誤是完全不同的東西?

+0

你有'void setId(std :: string p_id);',但是我沒有看到''的包含。 – StoryTeller

回答

6

Topics.h包括TDPoint.hTrack.h

TDPoint.h包括Topics.h

Track.h包括Topics.hTDPoint.h

這感覺就像一個圓形包括...你應該要麼向前聲明你的類來解決這個問題或修改Topics.h以使其不具有圓形。

+0

謝謝,我懷疑它是那麼明顯。我認爲包括衛兵會解決這個問題。現在我明白了多重和循環包含的區別 - 包括警衛不能防止後者。 仍然難過地看到來自Rhapsody的自動生成的代碼,這是一個簡單的錯誤。 謝謝! –

3

你有夾雜:文件Track.h包括Topics.h包括TDPoints.h包括Topics.h包括Track.h其中TDPoint類沒有聲明。

事實上,TDPoint.h根本不需要任何頭文件,它是完全獨立的(根據您的問題中顯示的代碼)。

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

常規提示:在頭文件中包含儘可能少的頭文件。

+0

謝謝,可悲的是我只能選擇1個答案 - 你是相同的,我選擇了第一個答案。 –

相關問題