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;
}
Related
What I have is 2 classes:
A.h:
#include "B.h"
class A
{
vector<B*> arr;
void Update(int32 id){...};
}
B.h
#include "A.h"
class B
{
int32 id;
A* parent;
void OnRemove()
{
...
parent->Update(id);
}
}
With that logic everything must work fine as expected.
But it won't because of loop include: A.h including B.h and B.h including A.h
The question is how to make it working with that structure of code or other.
Main feature that should exist is to call event in A object which is holding B object.
Every help would be appreciated.
Forward declare class A; in B.h and class B; in A.h
You should then move the implementation of onRemove() to B.cpp and include A.h here.
Also don't forget your include guards. Example:
#ifndef _A_H_
#define _A_H_
class B;
class A {
};
#endif
The include guards could also be replaced by #pragma once at the beginning of the header, this is a little less verbose.
Edit
To be complete:
// A.h
#pragma once
#include <vector>
class B;
class A {
std::vector<B*> arr;
public:
void Update(int32 id);
};
// A.cpp
#include "A.h"
// possibly #include "B.h" if necessary
void A::Update(int32 id) {
// impl ...
}
// B.h
#pragma once
class A;
class B
{
int32 id;
A* parent;
public:
void OnRemove();
};
// B.cpp
#include "B.h"
#include "A.h"
void B::OnRemove() {
parent->Update(id);
}
Well, something like this...
the usual way is to provide guarding macros to prevent recursion and move definition of functions in a separate file if needed:
a.h
#ifndef _A_H_
#define _A_H_
#include "B.h"
class A
{
vector<B*> arr;
void Update(int32 id){...};
}
#endif
...
c.cpp (if needed)
#include <a.h>
#include <b.h>
void B::onRemove() {
blahblahblah
}
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.
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;
}
I have a strange problem when I create a static function in class A and I want to call it from class B function. I get
undefined reference to `A::funcA(int)'
Here is my source code :
a.cpp
#include "a.h"
void funcA(int i) {
std::cout << i << std::endl;
}
a.h
#ifndef A_H
#define A_H
#include <iostream>
class A
{
public:
A();
static void funcA( int i );
};
#endif // A_H
b.cpp
#include "b.h"
void B::funcB(){
A::funcA(5);
}
and b.h
#ifndef B_H
#define B_H
#include "a.h"
class B
{
public:
B();
void funcB();
};
#endif // B_H
I'm compiling with Code::Blocks.
#include "a.h"
void funcA(int i) {
std::cout << i << std::endl;
}
should be
#include "a.h"
void A::funcA(int i) {
std::cout << i << std::endl;
}
Since funcA is a static function of your class A. This rule applies both to static and non-static methods.
You forgot to prefix the definition with the class name :
#include "a.h"
void A::funcA(int i) {
^^^
//Add the class name before the function name
std::cout << i << std::endl;
}
The way you did things, you defined an unrelated funcA(), ending up with two functions (namely A::funcA() and funcA(), the former being undefined).
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.