A.h
#ifndef A_H
#define A_H
#include "B.h"
class A{
public:
B* b;
A(){
b->ownership = this;
};
};
#endif
B.h
#ifndef B_H
#define B_H
class A;
class B{
public:
A* ownership;
};
#endif //B_H
main.cpp
#include "A.h"
class C{
A a1;
A a2;
};
int main()
{
C c;
return 0;
}
Commands:
g++ -g main.cpp -o main
./main
This program fails with segmentation fault.
"gdb main core" told me that error was in string: "b->ownership = this;"
Question: Where is my fault? What I should know not to make more of these errors? Thank you.
The problem is in this class:
class A
{
public:
B* b;
A()
{
b->ownership = this;
}
};
You're dereferencing b but you haven't created an instance of B.
Related
Is it possible to avoid circular dependency in the following header files without turning data member b1 in class A to a pointer/reference, and without relaxing the inline function requirement in class B?
A.h:
#ifndef A_H
#define A_H
#include <B.h> // Required, as data member b1 is not a pointer/reference
class A {
public:
B b1; // I want to keep this as as it is.
int m_a;
};
#endif
B.h:
#ifndef B_H
#define B_H
#include <A.h> // Required, as f() calls a member function of class A
class B {
public:
int f(A &a){return a.m_a;} // I want this to be an inline function.
};
#endif
...and let's say main.ccp is:
#include <iostream>
#include <A.h>
#include <B.h>
int main() {
A a;
B b;
std::cout << "Calling b.f(a): " << b.f(a) << std::endl;
return 0;
}
You could use this:
A.h
#include <B.h>
#ifndef A_H
#define A_H
class A
{
public:
B b1;
int m_a;
};
#endif // A_H
B.h
#ifndef B_H
#define B_H
class A;
class B
{
public:
int f(A &a);
};
#include <A.h>
inline int B::f(A &a)
{
return a.m_a;
}
#endif // B_H
main.cpp
#include <iostream>
#include <A.h> // these could be in any order
#include <B.h>
int main()
{
A a;
B b;
std::cout << "Calling b.f(a): " << b.f(a) << std::endl;
return 0;
}
UPDATE:
Now I have this, and it does not compile:
A.h:
#ifndef A_H
#define A_H
class A {
private:
int foo;
public:
A();
int getfoo();
};
#endif
A.cpp:
#include "A.h"
A::A() {
foo = 5;
}
int A::getfoo(){
return foo;
}
B.h:
#ifndef B_H
#define B_H
class B {
private:
A myA;
public:
B();
int getAvalue();
};
#endif
B.cpp:
#include "A.h"
#include "B.h"
int B::getAvalue(){
return myA.getfoo();
}
Errors:
b.h line 6: C2146: missing ';' before identifier 'myA'
b.h line 6: C4430: missing type specifier - int assumed
b.h line 6: C4430: missing type specifier - int assumed
END UPDATE
I have written 2 classes in different cpp and header files: class A and class B.
Class B uses class A as a private variable and the default constructor of class A is never called.
Here is my code:
A.h:
class A {
public:
A();
int getfoo();
};
A.cpp:
class A {
private:
int foo;
public:
A();
int getfoo();
};
A::A() {
foo = 5;
}
int A::getfoo(){
return foo;
}
B.h:
class B {
public:
int getAvalue();
};
B.cpp:
#include "A.h"
class B {
private:
A myA;
public:
int getAvalue();
};
int B::getAvalue(){
return myA.getfoo();
}
classtest.cpp:
#include "stdafx.h"
#include <iostream>
#include "B.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
B stackB;
cout << stackB.getAvalue() << endl;
B* storeB = new B();
cout << storeB->getAvalue() << endl;
cin.get();
return 0;
}
The output is never 5 and the breakpoint inside the constructor A::A() is never triggered. It doesn't matter if I use B globally or locally. This sample works totally fine if I put the classes and functions in one single file.
If I add an empty default constructor to class B, the default constructor of class A gets called, but then Visual Studio 2008 complains about stack corruption around variable stackB.
What am I doing wrong?
Just with this class alone:
A.h:
class A {
public:
A();
int getfoo();
};
A.cpp:
class A {
private:
int foo;
public:
A();
int getfoo();
};
A::A() {
foo = 5;
}
int A::getfoo() {
return foo;
}
You are declaring class A in A.h.
Then in your implementation(cpp) file, you are then again declaring class A.
You are also forgetting to include A.h in A.cpp. If you included the header into the cpp; the compiler would of thrown out errors telling you what was wrong. You are also missing either header guards, or the pragma directive.
Your class should look like this:
A.h
#ifndef A_H
#define A_H
class A {
private:
int foo;
public:
A();
int getFoo() const; // const to return member and prevents modification
};
#endif // !A_H
A.cpp
#include "A.h" // you forgot to include the header
A::A() : // class constructor using it's member initializer list
foo( 5 ) {
}
int A::getFoo() const {
return foo;
}
Now once you fix your class, then working on class B should not be a problem. However there is one thing to be careful of when including a header file of one class into another; you can end up with circular includes. The best way to prevent that is to use a class prototype in the header and include its header in the containing class's cpp file. There are some cases where a class proto type will not work, but I'll leave that up to you to do the research.
How to use Class Prototype in C++
Class B might look like this:
B.h
#ifndef B_H
#define B_H
// #include "A.h" // uncomment this if prototype below doesn't work.
class A; // class prototype may not work in all cases;
// If the above prototype does not work; comment it out
// and replace it with #include "A.h".
class B {
private:
A myA;
public:
B(); // remove default
int getAValue() const;
};
#endif // !B_H
B.cpp
#include "B.h"
#include "A.h" // If class A's prototype in the header does not work
// then comment this out and place it in this class B's header by
// replacing it with the prototype.
B::B() {} // default constructor (should make this complete type)
int B::getAValue() const {
return myA.getFoo();
}
And this should help to fix your problems. If using the class prototype does not work in the header because in some cases it may not; you can remove the prototype declaration from the header and replace it with the include directive of that class and remove its include from the cpp file.
When I build and run this code in Eclipse CDT, I expect it to print 1. It prints -1992206527 instead. Why is this?
Here's my code.
A.h
#ifndef A_H
#define A_H
class A {
private:
int a;
public:
A();
~A();
void printNum();
};
#endif
A.cpp
#include <iostream>
#include "A.h"
A::A() :
a(1)
{
}
A::~A(){}
void A::printNum(){
std::cout << a << std::endl;
}
B.h
#ifndef B_H_
#define B_H_
class A;
class B {
private:
A* obj;
public:
B();
~B();
int run();
};
#endif
B.cpp
#include "A.h"
#include "B.h"
B::B() {}
B::~B() {}
int B::run(){
obj->printNum();
return 0;
}
main.cpp
#include "B.h"
int main(int argc, char **argv) {
B b;
return b.run();
}
To make it print 1, you must initialize B::obj. Below is a B.cpp with obj being properly initialized/ deallocated.
#include "A.h"
#include "B.h"
B::B() : obj(new A) {}
B::~B() { delete obj;}
int B::run(){
obj->printNum();
return 0;
}
Alternatively, you could make obj be an object type A(i.e declare obj as A obj;), instead of making it a pointer to an object of type A. This would necessitate including A.hin B.h.
Is it possible to avoid circular dependency in the following header files without turning data member b1 in class A to a pointer/reference, and without relaxing the inline function requirement in class B?
A.h:
#ifndef A_H
#define A_H
#include <B.h> // Required, as data member b1 is not a pointer/reference
class A {
public:
B b1; // I want to keep this as as it is.
int m_a;
};
#endif
B.h:
#ifndef B_H
#define B_H
#include <A.h> // Required, as f() calls a member function of class A
class B {
public:
int f(A &a){return a.m_a;} // I want this to be an inline function.
};
#endif
...and let's say main.ccp is:
#include <iostream>
#include <A.h>
#include <B.h>
int main() {
A a;
B b;
std::cout << "Calling b.f(a): " << b.f(a) << std::endl;
return 0;
}
You could use this:
A.h
#include <B.h>
#ifndef A_H
#define A_H
class A
{
public:
B b1;
int m_a;
};
#endif // A_H
B.h
#ifndef B_H
#define B_H
class A;
class B
{
public:
int f(A &a);
};
#include <A.h>
inline int B::f(A &a)
{
return a.m_a;
}
#endif // B_H
main.cpp
#include <iostream>
#include <A.h> // these could be in any order
#include <B.h>
int main()
{
A a;
B b;
std::cout << "Calling b.f(a): " << b.f(a) << std::endl;
return 0;
}
My code looks something like this:
main.cpp
#include <iostream>
#include "A.h"
#include "B.h"
using namespace std;
int main(){
int d,f;
A c();
d = c.GetStuff();
B *d = new C();
f = d->Get();
return 0;
}
A.h
#ifndef A_H
#define A_H
class A
{
int a;
public A();
int GetStuff() {return(a) ;}
};
#endif
A.cpp
#include "A.h"
A::A()
{
a = 42;//just some value for sake of illustration
}
B.h
#ifndef B_H
#define B_H
Class B
{
public:
virtual int Get(void) =0;
};
class C: public B {
public:
C();
int Get(void) {return(a);}
};
#endif
B.cpp
#include "B.h"
C::C() {
a // want to access this int a that occurs in A.cpp
}
My question is, what is the best way to gain access to "a" in B.cpp?
I tried using class "friend", but I am not getting results.
Any suggestions?
Thanks!
Two different answers, depending on what you mean
If each A object is meant to have it's own unique 'a' variable (which is how you've defined it) then you'll need to pass an A into the constructor of C:
C::C(const A &anA) {
int foo= anA.a; //
}
And, invoking the constructor becomes:
A myA;
B *myC = new C(myA); // You picked confusing names for your classes and objects
However, if you intended all A objects to share a common a value, then you should declare a and getStuff as static in A :
class A
{
static int a;
public:
static int GetStuff() {return a;};
... and access it as A::GetStuff() in the C constructor.