I'm having trouble defining and using two codependent classes in a C++/CLI library.
Based off of the answer to this question: Visual C++ (C++/CLI) Forward Declaration with Derivative Classes?
I have this: A.h
#pragma once
#include "B.h"
#ifdef B_H
namespace TestForwardDeclaration
{
public ref class A
{
public:
A()
{
}
double Result;
void Test(B ^b)
{
this->Result = b->Result;
}
};
}
#endif
B.h
#define B_H
#pragma once
ref class A;
namespace TestForwardDeclaration
{
public ref class B
{
public:
B()
{
}
double Result;
void Test(A ^a)
{
this->Result = a->Result;
}
};
}
#include "A.h"
This compiles and seems to have the functionality that I need, but I'm having trouble correctly implementing in a different file such as the main DLL file. if I include either A.h, B.h, or both, I get the error "use of undefined type 'A'.
What am I missing?
EDIT
I have yet to see a clear and full example of what seems to be a popular problem in C++ (using codependent classes), so here is the solution to my specific use case:
A.h
#pragma once
#include "B.h"
#ifdef B_H
namespace TestForwardDeclaration
{
public ref class A
{
public:
A();
double Result;
void Test(B ^b);
};
}
#endif
B.h
#define B_H
#pragma once
namespace TestForwardDeclaration
{
ref class A;
public ref class B
{
public:
B();
double Result;
void Test(A ^a);
};
}
#include "A.h"
TestForwardDeclaration.cpp
// This is the main DLL file.
#include "stdafx.h"
#include "A.h"
#include "B.h"
namespace TestForwardDeclaration
{
A::A()
{
}
void A::Test(B ^b)
{
this->Result = b->Result;
}
B::B()
{
}
void B::Test(A ^a)
{
this->Result = a->Result;
}
}