我想知道为什么代码下面不工作。 在b.cpp中,类B使用类A,但由于未找到类A的声明而失败。 但是,之前包含a.hpp。为什么#include“a.hpp”在这里不起作用?标题包括和循环依赖
感谢您的帮助!
//===============================================
//file: a.hpp
//===============================================
#ifndef _A_HPP
#define _A_HPP
#include "b.hpp"
class A{
public:
A();
// others methods using B here
};
#endif
//===============================================
//file: a.cpp
//===============================================
#include "a.hpp"
A::A(){}
//===============================================
//file: b.hpp
//===============================================
#ifndef _B_HPP
#define _B_HPP
#include "a.hpp"
class B{
public:
B(A a);
};
#endif
//===============================================
//file: b.cpp
//===============================================
#include "b.hpp"
B::B(A a){}
SHELL$ g++ -c a.cpp
In file included from a.hpp:7,
from a.cpp:4:
b.hpp:11: error: expected ‘)’ before ‘a’
感谢的好评约_ – user744629