错误消息:纯抽象类包装在 C++ 的命名空间中

发布于 2025-01-02 19:13:59 字数 1886 浏览 0 评论 0原文

需要帮助通过继承实现纯抽象类使用命名空间来包装我的所有类以避免与其他类发生冲突。

如果我从抽象类和从纯抽象类继承的所有类中删除命名空间包装器,我就能够成功构建和运行代码。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷途知返 2025-01-09 19:13:59

问题是您的前向类已在另一个名称空间(特别是全局名称空间)中声明。因此,要覆盖的虚拟是具有不同参数类型的不同符号。

正如所写,编译器将 ConcreteClass 与它在基类中声明时在全局命名空间中看到的前向声明进行匹配:

virtual bool Method001( const ConcreteClass &cc ) = 0;

当您在 ConcreteClass 中声明 Method001 时:

virtual bool Method001( const ConcreteClass &cc );

编译器将 ccMyCustomNamespace::ConcreteClass 匹配,因为这是更准确的匹配。

要解决此问题,只需将前向声明放置在正确的命名空间中即可:

namespace MyCustomNamespace {
  class 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:

virtual bool Method001( const ConcreteClass &cc ) = 0;

When you declare Method001 in ConcreteClass:

virtual bool Method001( const ConcreteClass &cc );

the compiler matches cc to MyCustomNamespace::ConcreteClass because that is a more accurate match.

To resolve the issue, just place your forward declaration in the proper namespace:

namespace MyCustomNamespace {
  class ConcreteClass;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文