Why does I encounter a compileerror when doing this.

Compile error says.
'Form3' : undeclared identifier
'Form4' : undeclared identifier

Inside of Form4.h:

#include "Form3.h"

Form3 ^form3 = gcnew Form3;
form3->ShowDialog();

Inside of Form3.h:

#include "Form4.h"

Form4 ^form4 = gcnew Form4;
form4->ShowDialog();

Recommended Answers

All 12 Replies

because you have recursive includes -- form3.h includes form4.h which includes form3.h ...

Not a good idea, as you have found out.

That shouldn´t be a good idéa so I get over many examples how to go around this problem.
In somehow I have to use any form of a forward declaration. This is my code so far.

Still I have the compilerror for:
'Form22' : no appropriate default constructor available

I have followed an example but are not defenetive of what is going on in this code and would greatly appreciate any guidance in the code in order to really understand it.


Inside Form3.h

ref class Form4;
ref class Form3
{
public:
	Form4 ^SomeMethodA();
};

Inside Form3.cpp

#include "stdafx.h"
#include "Form3.h"
#include "Form4.h"

Form4 ^Form3::SomeMethodA()
{
return gcnew Form4;
}

Inside Form4.h

ref class Form3;
ref class Form4
{
public:
	Form3 ^SomeMethodB();
};

Inside Form4.cpp

#include "stdafx.h"
#include "Form3.h"
#include "Form4.h"

Form3 ^Form4::SomeMethodB() //SomeMethodB is not found as a member ?
{
return gcnew Form4;
}

because you have recursive includes -- form3.h includes form4.h which includes form3.h ...

Not a good idea, as you have found out.

I think in the *.cpp implementation files you need to add using namespace <namespace name>; . That made it work in my test program.

Okay, thank you. In your testprogram, did you try to open eachothers forms
or just compiling the code without the code that open forms ?

I could wonder what this meen. using namespace <namespace name>; If I use that line in the *.cpp files, I will have the errors:
1>.\Form3.cpp(5) : error C2059: syntax error : '<'
1>.\Form4.cpp(6) : error C2059: syntax error : '<'

If I dont use the using namespace <namespace name>; line and dont use the codes that will open eachothers forms, it will compiles fine. I dont know what this leaves the problem or solution.


I think in the *.cpp implementation files you need to add using namespace <namespace name>; . That made it work in my test program.

Replace the "<namespace name>" with the namespace used in your programs. You can find this in the *.h file, for example

#pragma once

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

#include "Form1.h"

namespace testform {

Yours will be different than mine -- the VC++ 2008 compiler generates the namespace name based on the project name when you created the project.

So I added this line: using namespace testform;

I didn´t understand that it was the projectname I should have written, sorry for that.
My projectname is simply "Form1" as in your testprogram "testform"
So now I guess I have written the *.cpp files correct. But still there is errors.

Form3.cpp

#include "stdafx.h"
#include "Form3.h"
#include "Form4.h"

Form4 ^Form3::SomeMethodA()
{
return gcnew Form4;
}

using namespace Form1;

Form4.cpp

#include "stdafx.h"
#include "Form3.h"
#include "Form4.h"

Form3 ^Form4::SomeMethodB()
{
return gcnew Form3;
}

using namespace Form1;

Exactly the same compileerror situation occurs anyway with this in place but I
will only have these compileerrors if I use these codes that would open eachothers
forms and the compiler only gives errors for Form4 and not Form3:
I dont know if there could be any more clues to the error.

'Form4' : no appropriate default constructor available
use of undefined type 'Form4'
left of '->ShowDialog' must point to class/struct/union/generic type


Form3.h

Form4 ^form4 = gcnew Form4;
form4->ShowDialog(); //open form4

Form4.h

Form3 ^form3 = gcnew Form3;
form3->ShowDialog(); //open form3

zip up the project minus object files and post it so that we can see what the problem is.

Thank You, I have zipped the project, the object files only took 18 kB unzipped so I left them there in case anyway.
I removed a file named: VC++ Intellisense DataBase wich took up 10.9 MB of memory.
It was not possible to upload the file with this in the folder.
I hope I am sending the project correctly.

zip up the project minus object files and post it so that we can see what the problem is.

move the implementation of button1_Click() from Form3.h to Form3.cpp. Attached are new files. It still has link problems for unresolved external functions, but you just have to implenent them in the *.cpp files.

I appreciate the effort on this help but there is to many problems for me to localize how this really will be done.
I have managed to do one more thing and that is to move ths class code for Form3.h and Form4.h to the End of the .h files wich made it possible to now see the designView of these 2forms and it compiles.
I have put this code after the FormClass in the End of the .h files instead of before the FormClass in the beginning of the .h files:

ref class Form4;
ref class Form3
{
public:
	Form4 ^SomeMethodA();
};

But if I will move the implemenation of button1_Click() from Form3.h to Form3.cpp like this, I will have compileerrors:
'button1_Click' : undeclared identifier
'button1_Click' : is not a member of 'Form1::Form3'
'System::EventHandler' : a delegate constructor expects 2 argument(s)

#include "stdafx.h"
#include "Form3.h"
#include "Form4.h"

Form4 ^Form3::SomeMethodA()
{
return gcnew Form4;
}

using namespace Form1;

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
{
 	  Form4 ^form4 = gcnew Form4;
	  form4->Show();
}

I have attached a new .zip where the designView now can be seen.
One more thing is that the compiler doesn´t produce any .exe file when it compiles fine where I can run the program but this is perheps because of something that isn´t correct anyway that it compiles.

move the implementation of button1_Click() from Form3.h to Form3.cpp. Attached are new files. It still has link problems for unresolved external functions, but you just have to implenent them in the *.cpp files.

didn't you look at the code I gave you????

ref class Form4;
ref class Form3
{
public:
	Form4 ^SomeMethodA();
};

take that out of the *.h files because it isn't needed.

Now I can see the code. If I delete my project in the Visual Studio2008/Projects and just put the project I got from you and open the project under "Recent Projects" then my deleted project is loaded, very strange ? So I saw my old project and not yours.
But I did open it manually and now I can see what you did. As I am quite new to this I have to ask what is ment by this.

>> It still has link problems for unresolved external functions, but you just have to implenent them in the *.cpp files.

What is "unresolved external functions" ?
Sorry for all these questions but I find it difficult as it is so many things that has to be moved and put in order to "just" get this function to work.

As the project get these errors:
1>Form4.obj : error LNK2005: "private: class Form1::Form4 ^ __clrcall Form1::Form3:: SomeMethodA(void)" (?SomeMethodA@Form3@Form1@@$$FA$AAMP$AAVForm4@2@XZ) already defined in Form3.obj

1>Form4.obj : error LNK2020: unresolved token (06000004) Form1.Form4::button1_Click
1>Form3.obj : error LNK2020: unresolved token (06000004) Form1.Form4::button1_Click
1>Form2.obj : error LNK2020: unresolved token (06000004) Form1.Form4::button1_Click
1>Form1.obj : error LNK2020: unresolved token (06000004) Form1.Form4::button1_Click
1>C:\Documents and Settings\Andreassss\My Documents\Visual Studio 2008\Projects\Form1\Debug\Form1.exe : fatal error LNK1120: 4 unresolved externals

didn't you look at the code I gave you????

ref class Form4;
ref class Form3
{
public:
	Form4 ^SomeMethodA();
};

take that out of the *.h files because it isn't needed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.