我想知道爲什麼代碼下面不工作。 在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