错误C2143:语法错误:缺少';'在'^'之前

时间:2017-04-13 17:24:54

标签: c++-cli

我有以下问题,我有一个类A,它有一个B类实例,B类有一个A类实例。在VisualStudio 2013中给我错误“错误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);
};

1 个答案:

答案 0 :(得分:3)

C ++ / CLI与C ++一样,使用单遍编译。因为两个头文件彼此包含,所以预处理器最终将其中一个首先放在一起,并且最终会出现一个错误,其中第二个类尚未定义。我确定您还收到有关未定义类的错误消息。

要解决此问题,请不要包含另一个头文件。包括.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);
};