2017-04-13 122 views
0

之前我有以下問題,我有一個具有類B實例的類A,而類B具有類A的實例。在VisualStudio 2013中給我提供了錯誤「錯誤C2143:語法錯誤:失蹤 ';' '^'之前「下面是班級代碼。由於事先錯誤C2143:語法錯誤:缺少';'在'^'

#include "stdafx.h" 
#include "BAsterNode.h" 

using namespace System; 
using namespace System::Collections::Generic; 

ref class BAsterInfo 
{ 
private: 
    IComparable^ info; 
    BAsterNode^ enlaceMayores; /* error C2143 */ 
public: 
    IComparable^ GetInfo(); 
    void SetInfo(IComparable^); 
    BAsterNode^ GetEnlaceMayores(); 
    void SetEnlaceMayores(BAsterNode^ enlaceMayoresP); 
}; 

和第其它類

#include "stdafx.h" 
#include "BAsterInfo.h" 

using namespace System; 
using namespace System::Collections::Generic; 

ref class BAsterNode 
{ 
private: 
    BAsterNode^ enlaceMenores; 
    List<BAsterInfo^>^ listaInformacion; 
     int Find(BAsterInfo^ info); 
public: 
    List<BAsterInfo^>^ GetListaInfo(); 
    void SetListaInfo(List<BAsterInfo^>^ listaInfoP); 
    BAsterNode^ GetEnlaceMenores(); 
    void SetEnlaceMenores(BAsterNode^ enlaceMenoresP); 
}; 

回答

3

C++/CLI,如C++,使用單通編譯。因爲這兩個頭文件都包含在一起,所以預處理器最終將其中的一個放在第一個,並且最後一個錯誤在第二個類尚未定義的地方結束。我相信你也會收到關於未定義類的錯誤消息。

要解決這個問題,請不要包含另一個頭文件。包含來自.cpp文件的兩個頭文件,並在每個頭文件中使用另一個類的前向聲明。這會讓你在各種方法聲明中使用其他類。您將需要.cpp中包含的頭文件來調用另一個類中的任何方法,因此如果您有任何使用頭文件中定義的其他類的函數,則需要將它們移動到.cpp文件中。 CPP。

#include "stdafx.h" 

using namespace System; 
using namespace System::Collections::Generic; 

// Forward declaration 
ref class BAsterInfo; 

ref class BAsterNode 
{ 
private: 
    BAsterNode^ enlaceMenores; 
    List<BAsterInfo^>^ listaInformacion; 
     int Find(BAsterInfo^ info); 
public: 
    List<BAsterInfo^>^ GetListaInfo(); 
    void SetListaInfo(List<BAsterInfo^>^ listaInfoP); 
    BAsterNode^ GetEnlaceMenores(); 
    void SetEnlaceMenores(BAsterNode^ enlaceMenoresP); 
};