错误消息:纯抽象类包装在 C++ 的命名空间中
需要帮助通过继承实现纯抽象类,使用命名空间来包装我的所有类以避免与其他类发生冲突。
如果我从抽象类和从纯抽象类继承的所有类中删除命名空间包装器,我就能够成功构建和运行代码。
Visual Studio 2010 编译器似乎在抱怨,尽管所有类都在同一命名空间中,但抽象类的纯抽象方法并未实现。
任何帮助将不胜感激。
//IBaseClass.h
//forward declaration
class ConcreteClass;
//namespace MyCustomNamespace
//{
class IBaseClass
{
public:
virtual ~IBaseClass() { /*virtual destructor*/ }
//Behaviours...
virtual bool Method001( const ConcreteClass &cc ) = 0;
//virtual bool Method002(/*some input*/) = 0;
};
//} /*NAMESPACE*/
//-----------------------------------------
//ParentClass.h
//namespace MyCustomNamespace
//{
class ParentClass : virtual public IBaseClass
{
private:
int a;
public:
virtual ~ParentClass() { /*virtual destructor*/ }
//getter-setter implemented in ParentClass.cpp file...
void setA(const int aa);
const int getA() const;
};
//} /*NAMESPACE*/
//-----------------------------------------
//ConcreteClass.h
//namespace MyCustomNamespace
//{
class ConcreteClass: public ParentClass
{
private:
int b;
public:
virtual ~ConcreteClass() { /*virtual destructor*/ }
//getter-setter...
void setB(const int bb);
const int getB() const;
bool Method001( const ConcreteClass &cc ); //re-declaring IBase abstract method...
};
//} /*NAMESPACE*/
//-----------------------------------------
//ConcreteClass.cpp
//namespace MyCustomNamespace
//{
void ConcreteClass::setB(const int bb) { this->b = bb; }
const int ConcreteClass::getB() const { return this->b; }
bool ConcreteClass::Method001( const ConcreteClass &cc )
{
//implementation code goes here...
return false;
}
//} /*NAMESPACE*/
Need help with implementing a pure abstract class via inheritance, using namespace to wrap all my classes to avoid conflict with others.
I have been able to build and run the code successfully if I remove namespace wrapper from my abstract class and all classes that inherit from my pure abstract class.
It seems like Visual Studio 2010 compiler is complaining that despite all classes are in the same namespace, the abstract class's pure abstract method is not implemented.
Any help would be much appreciated.
//IBaseClass.h
//forward declaration
class ConcreteClass;
//namespace MyCustomNamespace
//{
class IBaseClass
{
public:
virtual ~IBaseClass() { /*virtual destructor*/ }
//Behaviours...
virtual bool Method001( const ConcreteClass &cc ) = 0;
//virtual bool Method002(/*some input*/) = 0;
};
//} /*NAMESPACE*/
//-----------------------------------------
//ParentClass.h
//namespace MyCustomNamespace
//{
class ParentClass : virtual public IBaseClass
{
private:
int a;
public:
virtual ~ParentClass() { /*virtual destructor*/ }
//getter-setter implemented in ParentClass.cpp file...
void setA(const int aa);
const int getA() const;
};
//} /*NAMESPACE*/
//-----------------------------------------
//ConcreteClass.h
//namespace MyCustomNamespace
//{
class ConcreteClass: public ParentClass
{
private:
int b;
public:
virtual ~ConcreteClass() { /*virtual destructor*/ }
//getter-setter...
void setB(const int bb);
const int getB() const;
bool Method001( const ConcreteClass &cc ); //re-declaring IBase abstract method...
};
//} /*NAMESPACE*/
//-----------------------------------------
//ConcreteClass.cpp
//namespace MyCustomNamespace
//{
void ConcreteClass::setB(const int bb) { this->b = bb; }
const int ConcreteClass::getB() const { return this->b; }
bool ConcreteClass::Method001( const ConcreteClass &cc )
{
//implementation code goes here...
return false;
}
//} /*NAMESPACE*/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您的前向类已在另一个名称空间(特别是全局名称空间)中声明。因此,要覆盖的虚拟是具有不同参数类型的不同符号。
正如所写,编译器将
ConcreteClass
与它在基类中声明时在全局命名空间中看到的前向声明进行匹配:当您在
ConcreteClass
中声明Method001
时:编译器将
cc
与MyCustomNamespace::ConcreteClass
匹配,因为这是更准确的匹配。要解决此问题,只需将前向声明放置在正确的命名空间中即可:
The problem is that your forward class has been declared in another namespace (specifically, the global namespace). Thus, the virtual to override is a different symbol with a different parameter type.
As written, the compiler matches
ConcreteClass
to the forward declaration it sees in the global namespace when declared in the base:When you declare
Method001
inConcreteClass
:the compiler matches
cc
toMyCustomNamespace::ConcreteClass
because that is a more accurate match.To resolve the issue, just place your forward declaration in the proper namespace: