Eliezer
Eliezer

Reputation: 7347

C++ Scope Operator in Derivation List

I'm getting an error in WinImp.h that says 'Root' has not been declared. If I don't use the scope operator (class WinImp : public BaseDef) the error is error: expected class-name before '{' token). Anyone know why this is happening?

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

Upvotes: 1

Views: 212

Answers (2)

moswald
moswald

Reputation: 11677

Fix WinImp.cpp to look like this:

#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

Upvotes: 2

billz
billz

Reputation: 45410

You are accessing BaseDef interfaces in Root, so they suppose to be public:

class BaseDef 
{
 public:
   virtual void foo() = 0;
   virtual void bar() = 0;
};

In WinImp.cpp, foo(), bar() need return type and they are not inside Root scope, should be:

void WinImp::foo() { }
void WinImp::bar() { }

Upvotes: 2

Related Questions