【发布时间】:2012-02-19 01:13:51
【问题描述】:
我想知道为什么下面的代码不起作用。 在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’
【问题讨论】:
标签: c++ header dependencies