around circular dependencies
I have written intentionally two classes , 1 instance 2 and 2 instance 1.
My intention is discover if the project can build or not.
ok, the project can be compiled without errors.
But when run I have :
exited with code -1073741819 = STATUS_ACCESS_VIOLATION 0xC0000005
Can I'm sure that this message is because the circular dependencies?
Why not I get errors when build ? (I use mingw)
Thanks.
Re: around circular dependencies
Are you using forward declarations ?
Code:
// A.h:
class B; //!< declare class B, just to be able to use pointers
class A{
public:
A();
protected:
B * b;
};
// A.cpp
#include "A.h"
#include "B.h"
A::A(){
b = new B();
}
///////// same for class B //////////////
// B.h:
class A; //!< forward declare class A
class B{
public:
B();
protected:
A * a;
};
// B.cpp
#include "B.h"
#include "A.h"
B::B(){
a = new A();
}
This way there are no "circular" includes.
Show us how did you code it
Re: around circular dependencies
I'm have not forward intentionally.
My question are 'why the project can be build and have no errors' ?
I have not tools in my QT+mingw to check it?
Thanks
Re: around circular dependencies
I meant that forward declaring is somethig that can help you when you want to have class A using class B and the other way around.
How did you include the headers ? If you did somethig like:
Code:
// A.h:
#ifndef A
#define A
#include "B.h"
class A...
#endif
// B.h:
#ifndef B
#define B
#include "A.h"
class B...
#endif
then think about it, in order to use B in A, B should be declared before A ( and the other way around ). Which class is defined first and which is second in this case ? ( without seeing your include schema I can only guess how this looks like in your code )
Try the "forward declarations" solution proposed earlier.
Re: around circular dependencies
Yes, something like this.
As I said at the beggining of the post, I deliberately have write a code to rise an error with circular dependencies.
And my questión is: How can be possible I can build the project?
And this error means circular dependence error ?
exited with code -1073741819 = STATUS_ACCESS_VIOLATION 0xC0000005
and, a more question:
ClassA.h
Code:
class B;
class A {
public:
private:
}
ClassA.cpp
Code:
#include "classb.h"
ClassA::function(){
Classb *clasb;
clasb->function();
}
It works .
But this one not :
ClassA.h
Code:
class B;
class A {
public:
Classb *clasb; // I want to use in several places ...
private:
}
ClassA.cpp
Code:
#include "classb.h"
ClassA::function(){
clasb->function();
}
At the second case, I have :
Invalid use in static ClassA::clasb static member.
Is that than I cannot create public or private objetcs using forward?
Thanks
Re: around circular dependencies
If you are using an uninitialized pointer then why are you wondering why it crashes?
Re: around circular dependencies
Quote:
Originally Posted by
tonnot
Code:
#include "classb.h"
ClassA::function(){
clasb->function();
}
At the second case, I have :
Invalid use in static ClassA::clasb static member.
The above code fragment doesn't make sense at all. Forward declaration doesn't mean that you will declare the body of the function somewhere else. Read starting from here http://www.parashift.com/c++-faq-lit...html#faq-39.11