2017-04-13 129 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); 
}; 
相关问题