【发布时间】:2012-11-30 03:12:29
【问题描述】:
我有两个班级 A 和 B
A.hpp
#include <vector>
#include <algorithm>
#include "B.hpp"
class A {
public:
void sortTrans() { std::sort(trans_.begin(), trans_.end(), sortStruct); }
unsigned int name() { return name_; }
private:
std::vector<B*> trans_;
unsigned int name_;
};
B.hpp:
class A;
class B {
A& source_;
A& dest_;
unsigned int choice_;
};
现在我想按选择和名称的值对 trans_ 进行排序,因此我写了
struct sort {
bool operator()(B* t1, B* t2) {
if (t1->choice < t2->choice)
return true;
if (t1->dest_.name() < t2->dest_.name())
return true;
return false;
}
} sortStruct;
但现在我面临着打破循环依赖的问题。 A的定义在A.hpp中,B的定义在B.hpp中。在 B.hpp 中,我使用 A 的前向减速,A 包括 B.hpp。但是我必须在哪里(或如何)放置 sortStruct,因为它使用 A 和 B 的定义。而且我总是收到错误
Wrong usage of forward declaration A
感谢您的帮助。
【问题讨论】:
-
您能否向我们展示 A.hpp 和 B.hpp 的全部内容,包括前向声明
-
当然,我编辑了我的问题
-
我不确定,但this help ??
-
您的
B没有您调用的attr1和attr2方法,并且您的sort::operator()根本没有引用A。你的实际代码是什么样的,编译器抱怨哪一行? -
还告诉哪个编译器,哪一行。有帮助!
标签: c++ g++ circular-dependency