|
-
Mar 11th, 2002, 11:44 PM
#1
Thread Starter
Fanatic Member
circular definitions
its late and i don't feel like thinking about how i need to go about doing this so anyone who has run into this problem before could you please help?
i have two classes that refer back to each other and it can't compile in the normal fashion
thanx
The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.
-
Mar 12th, 2002, 08:14 AM
#2
What is the error?
Are you using pointers to the other class, because you might be able to use forward declarations...
-
Mar 12th, 2002, 11:15 AM
#3
class b;
class a {
b* pb; };
class b {
a* pa; };
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 12th, 2002, 03:02 PM
#4
Thread Starter
Fanatic Member
yes, thank you, that works
the only problem is im using a member function from one of the classes and its telling me the function doesn't exist?
would i need to do something like this?
class b
{
void nothing();
};
class a
{
void func()
{b->nothing();}
b* pb;
};
class b
{
void nothing()
{ cout << "nothing";}
a* pa;
};
The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.
-
Mar 12th, 2002, 03:22 PM
#5
Originally posted by CornedBee
class b; <- This is a forward declaration.
class a {
b* pb; };
class b {
a* pa; };
That was just for DNA's info...
Umm the only thing I can suggest is... Do not "inline" the function that way. Move its definition into a source file. If you still want it inline you can specify that by placing the keyword "inline" before the function header.
-
Mar 12th, 2002, 03:24 PM
#6
Thread Starter
Fanatic Member
ok, well the function is in a seperate file, i just did that for my little example
The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.
-
Mar 12th, 2002, 04:24 PM
#7
Thread Starter
Fanatic Member
ok i think i got it now
i need to include all the .cpp code after ALL of the headers have been defined
The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.
-
Mar 12th, 2002, 04:29 PM
#8
Thread Starter
Fanatic Member
i was wrong, it still doesn't work
i wish there were more people on this forum
The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.
-
Mar 13th, 2002, 08:49 AM
#9
always include all headers first.
inline functions are static by definition, so
Code:
// a .h file, included by both .cpp files
class a{
public:
void foo();
};
// one .cpp file:
inline void a::foo()
{
// code
}
// another .cpp file:
void main()
{
a obj;
obj.foo(); // linker error!
}
The functions only exist in the module they were defined in. Solution: put the functions in a special file (usual extension is .inl), then include this file at the end of the header.
Code:
// a.h
class a{
public:
void foo();
};
#include "a.inl"
// a.inl
inline void foo()
{
// foo
}
// some.cpp
void main()
{
a obj;
obj.foo(); // OK
}
// someother.cpp
void func()
{
a obj;
obj.foo(); // OK
}
You could also place the code in a.inl directly in a.h, but the separation makes it more readable.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 24th, 2005, 06:24 PM
#10
Addicted Member
Re: circular definitions
How about:
class a
{
b thisb; // not a pointer!
}
class b
{
a* pa;
}
???
ParadoxResolved
Go ahead, computer! Give me another Access Violation. . . I dare you!
Here to chafe
-
Jan 24th, 2005, 06:26 PM
#11
Re: circular definitions
Possible, but you must reorder:
Code:
class a;
class b {
a *pa;
};
class a {
b ob;
};
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 24th, 2005, 06:40 PM
#12
Addicted Member
Re: circular definitions
Not sure why, but that doesn't seem to compile. . .
//{{AFX_INCLUDES()
#include "MyStatic.h"
//}}AFX_INCLUDES
class CInterfaceView : public CFormView
{
...
//{{AFX_DATA(CInterfaceView)
...
CMyStatic m_Att1Val;
...
//}}AFX_DATA
...
}
#include "interfaceView.h"
class CInterfaceView;
class CMyStatic: public CStatic
{
...
CInterfaceView* parentform;
...
}
That leads to over a thousand errors.
ParadoxResolved
Go ahead, computer! Give me another Access Violation. . . I dare you!
Here to chafe
-
Jan 24th, 2005, 06:44 PM
#13
Re: circular definitions
Remove the #include "interfaceView.h" from MyStatic.h.
There's also no reason to store the pointer you're storing. Just use GetParent (or GetParentWindow) and cast the result.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 24th, 2005, 07:40 PM
#14
Addicted Member
ParadoxResolved
Go ahead, computer! Give me another Access Violation. . . I dare you!
Here to chafe
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|