I've 3 classes that belong to a namespace "MyNS". "A" uses the "B" and "C", while both "B" and "C" use "A". Here are the structure of my headers:
//mynamespace.h
#ifndef SOMENAMESPACE_H
#define SOMENAMESPACE_H
namespace MyNS {
class A;
class B;
class C;
}
#endif
/*******************************************************/
//A.h
#ifndef A_H
#define A_H
#include "mynamespace.h"
#include "B.h"
#include "C.h"
class MyNS::A {
..
MyNS::B someMethod(args);
MyNS::C someMethod2(args);
..
};
#endif
/*******************************************************/
//B.h
#ifndef B_H
#define B_H
#include "mynamespace.h"
#include "A.h"
class MyNS::B {
..
MyNS::A *someMember;
..
};
#endif
/*******************************************************/
//C.h
#ifndef C_H
#define C_H
#include "mynamespace.h"
#include "A.h"
class MyNS::C {
..
MyNS::A *someMember;
..
};
#endif
Each of the classes has a C++ source file that includes the line:
using namespace MyNS;
The compiler is stopped by the B.h header guards, that's why I get an "incomplete type 'class B'" error in A::someMethod(args).
I know I could have included all the class definitions inside "mynamespace.h", but the classes are long which made my header file bloated, that's why I had to separate them like this.
How can I make this code work with separate header files ?
The correct way to do this is using forward declaration. Do not include A.h in C.h and B.h. Only actually perform #include "A.h" in B.cpp and C.cpp. The forward declaration in the namespace should be enough.
//mynamespace.h
#ifndef SOMENAMESPACE_H
#define SOMENAMESPACE_H
namespace MyNS {
class A;
class B;
class C;
}
#endif
/*******************************************************/
//A.h
#ifndef A_H
#define A_H
#include "mynamespace.h"
#include "B.h"
#include "C.h"
class MyNS::A {
..
MyNS::B someMethod(args);
MyNS::C someMethod2(args);
..
};
#endif
/*******************************************************/
//B.h
#ifndef B_H
#define B_H
#include "mynamespace.h"
class MyNS::B {
..
MyNS::A *someMember;
..
};
#endif
/*******************************************************/
//C.h
#ifndef C_H
#define C_H
#include "mynamespace.h"
class MyNS::C {
..
MyNS::A *someMember;
..
};
#endif
Related
I am new to C++ programming and I have been stucked in this for hours.
I have three header files: A.h, B.h and common.h and two .cpp files: A.cpp, B.cpp.
A.h
#ifndef A_H
#define A_H
#include"B.h"
class AA{
public:
void fun();
};
#endif
B.h
#ifndef B_H
#define B_H
#include <iostream>
#include "common.h"
using namespace std;
class BB{
public:
void fun2();
};
#endif
common.h
extern int c;
A.cpp
#include "A.h"
void AA::fun(){
cout<<"A"<<endl;
}
B.cpp
#include "B.h"
void BB::fun2(){
cout<<"FUN 2"<<endl;
}
main.cpp
#include "A.h"
int main(){
int c = 5;
AA a;
a.fun();
return 0;
}
Error: undefined reference to `AA::fun()'
Why am I getting this error? Can anyone explain it?
Thanks a lot.
There are three .h files
A.h:
#ifndef __A_H__
#define __A_H__
#include"Card.h"
#include"B.h"
struct A{
Card card;
.....
};
void getCards(A *a, int num);
#endif
B.h
#ifndef __B_H__
#define __B_H__
#include"Card.h"
#include"A.h"
struct B{
Card card;
.....
};
void getCards(A *a, B *b, int num);
#endif
Card.h
#ifndef __CARD_H__
#define __CARD_H__
struct Card{
int num;
char *type;
};
#endif
Since A.h and B.h includes each other, not all header files are included.
Please give me some advices.
As far as I can see, you don't need to include "B.h" in your "A.h" file. So remove it to reduce dependencies.
Including "A.h" in your "B.h" file also seems unnecessary. A simple forward declaration should be sufficient.
B.h
#ifndef __B_H__
#define __B_H__
#include"Card.h"
class A; // then you will have to include A.h in your B.cpp file
struct B{
Card card;
.....
};
void getCards(A *a, B *b, int num);
#endif
The C++ Premier I have doesn't say much about what I am about to ask, and this is what I got googling LINK:
When the compiler compiles the #include "example.h" line, it copies the contents of example.h into the current file.
So, if that's true, in the following example why B.h doesn't know about A.h ? Is it how the files are compiled ? Do I have to include A.h in every file that use it and then include every file that use A.h in the program.h that uses these files ?
In program.h
#include "A.h"
#include "B.h"
WARNING: VERY BAD CODE:
a.h
#ifndef A_H
#define A_H
#define SOME_LIT "string lit in A.h"
#endif
b.h
#ifndef B_H
#define B_H
#include <iostream>
void foo() { std::cout << SOME_LIT << '\n'; }
#endif
main.cpp
#include "a.h"
#include "b.h"
int main()
{
foo();
}
Prints:
$ ./a.out
string lit in A.h
So you can see b.h knows about the define in a.h. If you forgot the #include "a.h", or put it below #include "b.h", this would break.
As a general rule however, you should explicitly #include a header in any file you need it. That way you know that you only care about foo in main, so you just #include the foo header, which is b.h:
#include "b.h"
int main()
{
foo();
}
I have this problem Symbol 'A' could not be resolved in file B.h , I'm using Eclipse IDE for C/C++ Developers:
//B.h file
#ifndef __B_H__
#define __B_H__
#include "A.h"
class B: public cs::A{
};
#endif
that include A.h file:
//A.h file
#ifndef A_H_
#define A_H_
namespace cs{
class A {
};
}
#endif
What I'm missing here ?
You placed the class A inside a namespace, you should keep the namespace resolution while using it:
class B: public cs::A{
};
Or
//B.h file
#ifndef __B_H__
#define __B_H__
#include "A.h"
using namespace cs;
class B: public A{
};
#endif
Which isn't recommended (check Als's comment).
Also you can do this to avoid both keeping the whole namespace qualification every time you use A (which you should do in the first solution), and using all the namespace:
//B.h file
#ifndef __B_H__
#define __B_H__
#include "A.h"
using cs::A;
class B: public A{
};
#endif
class B: public cs::A{ };
^^^^^^
You need to provide the fully qualified name of class A.
Note that the class A is defined inside the namespace cs and hence you cannot just use A without namespace qualification.
You are using namespace cs, remember to use that again when declaring class B.
//B.h file
#ifndef __B_H__
#define __B_H__
#include "A.h"
class B: public cs::A{
};
#endif
I have one class, called A, and it has it's own header file. Then I have another class, called B, which also has it's own header file. They each have their own .cpp file where I implement all of their functions.
I'm trying to have class B have a variable of class type A as a private variable, but I keep getting the error 'A' does not name a type
My code looks like this:
main.h:
#ifndef MAIN_H
#define MAIN_H
#include "A.h"
#include "B.h"
#endif
main.cpp:
#include "main.h"
int main( int argc, char* args[]) {
B test;
}
A.h:
#ifndef A_H
#define A_H
#include "main.h"
class A {
public:
//public functions
private:
//private variables
};
#endif
B.h:
#ifndef B_H
#define B_H
#include "main.h"
class B {
public:
//public functions...
private:
A temp;
}
#endif
So all of my includes are in main.h, which includes A before B. B has a variable of type A, but it is included from being in main.h and B.h includes main.h. However, I keep getting an error saying:
error: 'A' does not name a type.
I've done some googling, and it seems like that means that A isn't defined when you use it, but it should be defined there since it's being included in main.h, right?
The problem is that A.h includes main.h, which includes B.h, which tries to use A.
The good way to organize your files would be this:
main.h:
// not needed
main.cpp:
#include "B.h" // for using class B
int main( int argc, char* args[]) {
B test;
}
A.h:
#ifndef A_H
#define A_H
// no includes needed ATM
class A {
//...
};
#endif
B.h:
#ifndef B_H
#define B_H
#include "A.h" // for using class A
class B {
//public functions...
}
#endif
That way, B.h is self-contained and can be used without having to include anything else before it. That's very important as soon as your project grows above the toy level it is at now. Why would anyone trying to use what header x.h provides need to know to also include f.h, m.h, and u.h?
The code you provide compiles properly if you add a ; at the end of B.h
A better way of doing it would be #include "A.h" in "B.h", instead of #include "main.h"
But it is probably unrelated to your problem.
That kind of error may also be confusing if you are using templates and forget "typename".
A.h includes Main.h at the top.
Main.h skips A.h because A_H is already defined, then includes B.h.
B.h tries to make use of A, but A.h hasn't finished compiling yet so the type isn't defined.