I just started programming in C++, and I've tried to create 2 classes where one will contain the other.
File A.h
:
#ifndef _A_h
#define _A_h
class A{
public:
A(int id);
private:
int _id;
B _b; // HERE I GET A COMPILATION ERROR: B does not name a type
};
#endif
File A.cpp
:
#include "A.h"
#include "B.h"
#include <cstdio>
A::A(int id): _id(id), _b(){
printf("hello
the id is: %d
", _id);
}
File B.h
:
#ifndef _B_h
#define _B_h
class B{
public:
B();
};
#endif
File B.cpp
:
#include "B.h"
#include <cstdio>
B::B(){
printf("this is hello from B
");
}
I first compile the B class and then the A class, but then I get the error message:
A.h:9: error: ‘B’ does not name a type
How do I fix this problem?
question from:
https://stackoverflow.com/questions/3608305/class-name-does-not-name-a-type-in-c 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…