我在WinImp.h中收到'Root' has not been declared
的错误。如果我不使用范围运算符(class WinImp : public BaseDef
),则错误为error: expected class-name before '{' token
)。任何人都知道为什么会这样吗?
Root.h
class Root {
public:
class BaseDef {
public:
virtual void foo() = 0;
virtual void bar() = 0;
};
private:
#ifdef _WIN32
friend class WinImp;
#else
friend class NixImp;
#endif
BaseDef* imp;
BaseDef* getImp();
public:
Root() : imp(getImp()) {}
void foo();
void bar();
};
ROOT.CPP
#include "Root.h"
void Root::foo() {
imp->foo();
}
void Root::bar() {
imp->bar();
}
WinImp.h
#ifdef _WIN32
#include "Root.h"
class WinImp : public Root::BaseDef {
public:
void foo();
void bar();
};
#endif
WinImp.cpp
#include "WinImp.h"
#ifdef _WIN32
void WinImp::foo() {
}
void WinImp::bar() {
}
Root::BaseDef* Root::getImp() {
return static_cast<BaseDef*>(new WinImp());
}
#endif
答案 0 :(得分:2)
您正在访问Root中的BaseDef
接口,因此他们认为是公开的:
class BaseDef
{
public:
virtual void foo() = 0;
virtual void bar() = 0;
};
在WinImp.cpp中,foo(), bar()
需要返回类型且它们不在Root范围内,应该是:
void WinImp::foo() { }
void WinImp::bar() { }
答案 1 :(得分:2)
修正WinImp.cpp
看起来像这样:
#include "WinImp.h"
#ifdef _WIN32
// WinImp is not scoped within Root
void WinImp::foo() {
}
void WinImp::bar() {
}
Root::BaseDef* Root::getImp() {
return dynamic_cast<BaseDef*>(new WinImp());
}
#endif