Reputation: 3
I have the following problem, I have a class A that has an instance of a class B and class B has an instance of class A. In VisualStudio 2013 gives me the error "error C2143: syntax error: missing ';' Before '^' "Below is the class code. Thanks in advance
#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);
};
and th other class
#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);
};
Upvotes: 0
Views: 1916
Reputation: 27864
C++/CLI, like C++, uses a single-pass compilation. Because both header files include each other, the preprocessor ends up putting one of them first, and that one ends up with an error where the second class isn't defined yet. I'm sure you're also getting an error message about an undefined class.
To fix this, don't include one header file from the other. Include both header files from your .cpp files, and use a forward declaration of the other class in each header file. This will let you use the other class in your various method declarations. You'll need the header file, included from the .cpp, to call any methods on the other class, so if you have any functions that use the other class defined in the header file, you'll need to move them to the .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);
};
Upvotes: 3