Class ProtoType
-
Hey everybody, I got problem with prototype. I do have 2 classes both inherited from CDialog. And these two have pointer, which points each other. It seems smt like this:
CChild : CDialog { CMain* parent; void doSmt(); }; CMain : CDialog { CChild* child; void doSmt(); };
I got a header file which includes Child first and Main after. OK now my problem is if I do not write class prototype of Main in the header file before including Child header file it gives me error: CMain undeclared identifier which makes sense. But, when I do write prototype:class CMain; #include "CChild.h" #include "CMain.h"
then it gives me use of undefined type CMain at the line which I call a function of CMain from CChild class. And when I do (try to) write a prototype of DoSmt function:class CMain; void CMain::doSmt(); #include "CChild.h" #include "CMain.h"
gives me the same error at line where I define function prototype. So I guess the question is, how can I define prototype of my class function :confused: Thanks.... :) -
Hey everybody, I got problem with prototype. I do have 2 classes both inherited from CDialog. And these two have pointer, which points each other. It seems smt like this:
CChild : CDialog { CMain* parent; void doSmt(); }; CMain : CDialog { CChild* child; void doSmt(); };
I got a header file which includes Child first and Main after. OK now my problem is if I do not write class prototype of Main in the header file before including Child header file it gives me error: CMain undeclared identifier which makes sense. But, when I do write prototype:class CMain; #include "CChild.h" #include "CMain.h"
then it gives me use of undefined type CMain at the line which I call a function of CMain from CChild class. And when I do (try to) write a prototype of DoSmt function:class CMain; void CMain::doSmt(); #include "CChild.h" #include "CMain.h"
gives me the same error at line where I define function prototype. So I guess the question is, how can I define prototype of my class function :confused: Thanks.... :)dehseth wrote:
class CMain; #include "CChild.h" #include "CMain.h" then it gives me use of undefined type CMain at the line which I call a function of CMain from CChild class.
What is the source file compiling (whenever this error occurs)?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
Hey everybody, I got problem with prototype. I do have 2 classes both inherited from CDialog. And these two have pointer, which points each other. It seems smt like this:
CChild : CDialog { CMain* parent; void doSmt(); }; CMain : CDialog { CChild* child; void doSmt(); };
I got a header file which includes Child first and Main after. OK now my problem is if I do not write class prototype of Main in the header file before including Child header file it gives me error: CMain undeclared identifier which makes sense. But, when I do write prototype:class CMain; #include "CChild.h" #include "CMain.h"
then it gives me use of undefined type CMain at the line which I call a function of CMain from CChild class. And when I do (try to) write a prototype of DoSmt function:class CMain; void CMain::doSmt(); #include "CChild.h" #include "CMain.h"
gives me the same error at line where I define function prototype. So I guess the question is, how can I define prototype of my class function :confused: Thanks.... :)you need the forward declaration in the declaration file, say in CMain.h, declare CMain.h: class CChild; // forward declaration. CMain : CDialog { CChild* child; void doSmt(); }; remove the forward declaration in the cpp(I Suppose) CMain.cpp:
dehseth wrote:
class CMain; #include "CMain.h" #include "CChild.h" void CMain::doSmt() { } #include "CChild.h" #include "CMain.h"
-
you need the forward declaration in the declaration file, say in CMain.h, declare CMain.h: class CChild; // forward declaration. CMain : CDialog { CChild* child; void doSmt(); }; remove the forward declaration in the cpp(I Suppose) CMain.cpp:
dehseth wrote:
class CMain; #include "CMain.h" #include "CChild.h" void CMain::doSmt() { } #include "CChild.h" #include "CMain.h"
-
dehseth wrote:
class CMain; #include "CChild.h" #include "CMain.h" then it gives me use of undefined type CMain at the line which I call a function of CMain from CChild class.
What is the source file compiling (whenever this error occurs)?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarkesource code is a lil bit long it makes more confusion if I paste it in here. this error occurs during compiling: I have 2 classes which points each other with pointers. During compile I include child.h first and main.h later. Since child class includes a main class pointer in itself it asks for the class. So I need to write a prototype for mainclass before including child class. When I write class CMain; it asks for Main class functions, cause child class calls function of main class. And here my problem starts cause compiler says undeclare identifier of CMain class... So how can I declare a working prototype of CMain class to compile whole code :confused:
-
yes that can happen for the other CChild.cpp, if you understand what forward declaration can do, you can alone fix the problem. if you want from me, say what are the files cpp, h and how you declare
-
source code is a lil bit long it makes more confusion if I paste it in here. this error occurs during compiling: I have 2 classes which points each other with pointers. During compile I include child.h first and main.h later. Since child class includes a main class pointer in itself it asks for the class. So I need to write a prototype for mainclass before including child class. When I write class CMain; it asks for Main class functions, cause child class calls function of main class. And here my problem starts cause compiler says undeclare identifier of CMain class... So how can I declare a working prototype of CMain class to compile whole code :confused:
Deferred declaration (the one you used for class
CMain
should work, but you've define methods containing references toCMain
methods insideCChild
source (not inside the header). for instance://FILE: B.h
class A;
class B
{
A * _pA;B(pA){_pA=pA;}
void DoSomethingWithA();
};
//////////////////////////////////
//FILE B.cpp
void B::DoSomethingWithA()
{
_pA->DoSomething();
}should work, while //FILE: B.h class A; class B { A * _pA; B(pA){_pA=pA;} void DoSomethingWithA() { _pA->DoSomething(); // ERROR, UNDEFINED REFERENCE } }; shouldn't. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
Deferred declaration (the one you used for class
CMain
should work, but you've define methods containing references toCMain
methods insideCChild
source (not inside the header). for instance://FILE: B.h
class A;
class B
{
A * _pA;B(pA){_pA=pA;}
void DoSomethingWithA();
};
//////////////////////////////////
//FILE B.cpp
void B::DoSomethingWithA()
{
_pA->DoSomething();
}should work, while //FILE: B.h class A; class B { A * _pA; B(pA){_pA=pA;} void DoSomethingWithA() { _pA->DoSomething(); // ERROR, UNDEFINED REFERENCE } }; shouldn't. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarkeok! I got a lil bit confused right now... You sound right. I need to include header files.. Well I did writye the source code bottom of header files which is wrong, I get that now! Now my problem continues... These are the files I have got: Main.cpp Child.cpp MSFLexGrid.cpp Application.cpp I include header files in each of them correctly. Compling each one one by one does not create any errors. But when I do Build, if says error LNK2005: "int __cdecl getSize(char *)" (?getSize@@YAHPAD@Z) already defined in App.obj cause it's already included in cpp file... So Do I have to write
#ifndef
directives to each header file :confused: How can I build without having any linker errors?? Thanks... :) -
ok! I got a lil bit confused right now... You sound right. I need to include header files.. Well I did writye the source code bottom of header files which is wrong, I get that now! Now my problem continues... These are the files I have got: Main.cpp Child.cpp MSFLexGrid.cpp Application.cpp I include header files in each of them correctly. Compling each one one by one does not create any errors. But when I do Build, if says error LNK2005: "int __cdecl getSize(char *)" (?getSize@@YAHPAD@Z) already defined in App.obj cause it's already included in cpp file... So Do I have to write
#ifndef
directives to each header file :confused: How can I build without having any linker errors?? Thanks... :)How did you define such function? Where?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
How did you define such function? Where?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke