Cross reference between classes
-
Hello folks, Can someone help me with this question: How can I make this work?
#include "stdafx.h" using namespace System; public ref class classX { private: classY ^m_refClassY; public: classX() { this->m_refClassY = gcnew classY(); } }; public ref class classY { private: classX ^m_refClassX; public: classY() { this->m_refClassX = gcnew classX(); } }; int main(array<System::String ^> ^args) { Console::WriteLine(L"Hello World"); return 0; }
Always when I try to compile this source, this error comes up.cross_reference.cpp(10) : error C2143: syntax error : missing ';' before '^'
cross_reference.cpp(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
cross_reference.cpp(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
cross_reference.cpp(15) : error C2039: 'm_refClassY' : is not a member of 'classX'
cross_reference.cpp(8) : see declaration of 'classX'
cross_reference.cpp(15) : error C2061: syntax error : identifier 'classY' -
Hello folks, Can someone help me with this question: How can I make this work?
#include "stdafx.h" using namespace System; public ref class classX { private: classY ^m_refClassY; public: classX() { this->m_refClassY = gcnew classY(); } }; public ref class classY { private: classX ^m_refClassX; public: classY() { this->m_refClassX = gcnew classX(); } }; int main(array<System::String ^> ^args) { Console::WriteLine(L"Hello World"); return 0; }
Always when I try to compile this source, this error comes up.cross_reference.cpp(10) : error C2143: syntax error : missing ';' before '^'
cross_reference.cpp(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
cross_reference.cpp(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
cross_reference.cpp(15) : error C2039: 'm_refClassY' : is not a member of 'classX'
cross_reference.cpp(8) : see declaration of 'classX'
cross_reference.cpp(15) : error C2061: syntax error : identifier 'classY'Forward declaration is the solution for circular dependencies. See the corrected code
ref class classY; // forward declaring classY
public ref class classX
{
private:
classY ^m_refClassY;public:
classX();
};public ref class classY
{
private:
classX ^m_refClassX;
public:
classY();
};classX::classX(){
this->m_refClassY = gcnew classY();
}classY::classY(){
this->m_refClassX = gcnew classX();
}In a real application, try to move the classes into separate files. :)
Navaneeth How to use google | Ask smart questions
-
Forward declaration is the solution for circular dependencies. See the corrected code
ref class classY; // forward declaring classY
public ref class classX
{
private:
classY ^m_refClassY;public:
classX();
};public ref class classY
{
private:
classX ^m_refClassX;
public:
classY();
};classX::classX(){
this->m_refClassY = gcnew classY();
}classY::classY(){
this->m_refClassX = gcnew classX();
}In a real application, try to move the classes into separate files. :)
Navaneeth How to use google | Ask smart questions
-
Forward declaration is the solution for circular dependencies. See the corrected code
ref class classY; // forward declaring classY
public ref class classX
{
private:
classY ^m_refClassY;public:
classX();
};public ref class classY
{
private:
classX ^m_refClassX;
public:
classY();
};classX::classX(){
this->m_refClassY = gcnew classY();
}classY::classY(){
this->m_refClassX = gcnew classX();
}In a real application, try to move the classes into separate files. :)
Navaneeth How to use google | Ask smart questions
Navaneeth (or anyone :), I posted this under "Circular Form Reference" subject but it's a very similar question. I had tried to add 'ref class Form2;' but now get a 'no appropriate default constructor available'. I was able to recreate my issue by simply creating two forms and adding a button to each that call the other form. So most of the code is the automaticly generated code from MS. I'm trying to keep the example simple by only incl. some code. /*Form1.h*/ #pragma once #ifndef First #define First #include "Form2.h" ref class Form2; private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { Form2 ^ frm = gcnew Form2; frm->Show(); this->Close(); } #endif /*Form2.h*/ #pragma once #ifndef Second #define Second #include "Form1.h" private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { Form1 ^ frm = gcnew Form1; frm->Show(); this->Close(); } #endif
modified on Tuesday, March 24, 2009 1:30 PM
-
Navaneeth (or anyone :), I posted this under "Circular Form Reference" subject but it's a very similar question. I had tried to add 'ref class Form2;' but now get a 'no appropriate default constructor available'. I was able to recreate my issue by simply creating two forms and adding a button to each that call the other form. So most of the code is the automaticly generated code from MS. I'm trying to keep the example simple by only incl. some code. /*Form1.h*/ #pragma once #ifndef First #define First #include "Form2.h" ref class Form2; private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { Form2 ^ frm = gcnew Form2; frm->Show(); this->Close(); } #endif /*Form2.h*/ #pragma once #ifndef Second #define Second #include "Form1.h" private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { Form1 ^ frm = gcnew Form1; frm->Show(); this->Close(); } #endif
modified on Tuesday, March 24, 2009 1:30 PM
You'll probably need to separate your code out of the H files into CPP files for at least one of your classes.
/*Form1.h*/
#pragma onceref class Form2;
ref class Form1 : public System::Windows::Forms::Form
{
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e);
}/*Form1.cpp*/
#include "Form1.h"
#include "Form2.h"
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
Form2 ^ frm = gcnew Form2;
frm->Show();
this->Close();
}/*Form2.h*/
#pragma once
#include "Form1.h"private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
Form1 ^ frm = gcnew Form1;
frm->Show();
this->Close();
}Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Navaneeth (or anyone :), I posted this under "Circular Form Reference" subject but it's a very similar question. I had tried to add 'ref class Form2;' but now get a 'no appropriate default constructor available'. I was able to recreate my issue by simply creating two forms and adding a button to each that call the other form. So most of the code is the automaticly generated code from MS. I'm trying to keep the example simple by only incl. some code. /*Form1.h*/ #pragma once #ifndef First #define First #include "Form2.h" ref class Form2; private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { Form2 ^ frm = gcnew Form2; frm->Show(); this->Close(); } #endif /*Form2.h*/ #pragma once #ifndef Second #define Second #include "Form1.h" private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { Form1 ^ frm = gcnew Form1; frm->Show(); this->Close(); } #endif
modified on Tuesday, March 24, 2009 1:30 PM
Hi, To do what do you want, you´ll have to do forward declaration, in the same way explained by Navaneeth.
/*Form1.h*/ #pragma once ref class Form2; // forward declaration /* prototype */ private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e); /*Form1.cpp*/ #include "Form1.h" #include "Form2.h" // here, you have to add the real declaration. System::Void Form1::button1_Click(System::Object^ sender, System::EventArgs^ e) { Form2 ^ frm = gcnew Form2; frm->Show(); this->Close(); } /*Form2.h*/ #pragma once #include "Form1.h" // Can be here becouse Form1.h don´t have a referece to this file. /* Can be inline */ private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { Form1 ^ frm = gcnew Form1; frm->Show(); this->Close(); }