-2

EDITED:

This question has already been asked here but didn't help in my case. I'm trying to have a hierarchy of classes, with inherited public update() functions. But I want a given derived derived class to call the functionality of all of its base classes before doing its own processing. My actual VS2013 solution consists of an EXE project that references a DLL project, but the simplified code below still produces the error:

// Map.h (in DLL project)
namespace Game2D {
    class Map {
    public:
        explicit Map();
        ~Map();
        void update(double);
    protected:
        virtual void baseUpdates(double dt) {}
    };
}

// Map.cpp (in DLL project)
namespace Game2D {
    Map::Map() { }
    Map::~Map() {}
    void Map::update(double dt) {
        baseUpdates(dt);
        // Do some base stuf...
    }
}

// AutoScrollMap.h (in DLL project)
namespace Game2D {
    class AutoScrollMap : public Map {
    public:
        explicit AutoScrollMap();
        ~AutoScrollMap();
    protected:
        virtual void baseUpdates(double) {}
    };
}

// AutoScrollMap.cpp (in DLL project)
namespace Game2D {
    AutoScrollMap::AutoScrollMap() : Game2D::Map() {}
    AutoScrollMap::~AutoScrollMap() {}
    void AutoScrollMap::baseUpdates(double dt) {
        // Do some stuff...
    }
}

// DesertMap.h (in EXE project)
namespace Shooter {
    class DesertMap : public Game2D::AutoScrollMap {
    public:
        explicit DesertMap();
        ~DesertMap();
    protected:
        virtual void baseUpdates(double);
    };
}

// DesertMap.cpp (in EXE project)
namespace Shooter {
    DesertMap::DesertMap() : Game2D::AutoScrollMap() {}
    DesertMap::~DesertMap() {}
    void DesertMap::baseUpdates(double dt) {
        AutoScrollMap::baseUpdates(dt);
        // Do more specific stuff...
    }
}

This gives me a compiler error of "error C2084: function 'void Game2D::AutoScrollMap::baseUpdates(double)' already has a body". The article above says I can use this syntax to call a function that is being overloaded. However, the base function in its example was public, not protected, and I really want to keep baseUpdates() protected since its not part of the interface.

I thought this problem was a fairly basic use of the OOP inheritance paradigm, so what am I missing? All advice is greatly appreciated!

Community
  • 1
  • 1
Rabadash8820
  • 2,328
  • 3
  • 27
  • 49
  • 2
    I don't see the problem. Please post an [MCVE](http://stackoverflow.com/help/mcve). – juanchopanza Feb 15 '15 at 16:13
  • An MCVE [like this one perhaps](http://ideone.com/SGc1so). – juanchopanza Feb 15 '15 at 16:23
  • I hoped the above code would be minimal/complete enough. My actual solution puts the Base and Derived1 classes in their own namespace, and they are exported from a DLL project. The Derived2 class is in another namespace in an EXE project that references the DLL. I am getting the linker error even without a main() function that actually insantiates any of these classes. – Rabadash8820 Feb 15 '15 at 16:56
  • The point is you claim something won't work (won't compile) but in my MVCE it does compile. So what is the problem you're trying to solve? – juanchopanza Feb 15 '15 at 17:47
  • Sorry it took so long, but I edited the code in the example, and it should now serve as valid MVCE. Thanks again for your help. – Rabadash8820 Feb 15 '15 at 18:48

2 Answers2

1

The method you described has no problem, the problem is in your code. You already implemented Derived2::baseUpdates() inline, then you try to define it again. You should change Derived2 to this:

class Derived2 : public Derived1 {                                              
    public:                                                                     
        Derived2() : Derived1() { }                                             
    protected:                                                                  
        virtual void baseUpdates();                                             
};                                                                              


void Derived2::baseUpdates() {                                                  
    Derived1::baseUpdates();                                                    
}

Also your Base constructor is not implemented.

swang
  • 5,157
  • 5
  • 33
  • 55
  • Thanks @swang. In my project, the classes are actually split into .h and .cpp files. Derived2.h just declares baseUpdates(), and I define it in Derived2.cpp. Same with Derived1. The problem seems to be that both Derived1::baseUpdates() and Derived2::baseUpdates() have bodies, but idk why that would be a problem – Rabadash8820 Feb 15 '15 at 18:03
  • no it shouldn't be, maybe it's more useful if you post your actual code that causes the error. – swang Feb 15 '15 at 18:07
  • Sorry it took so long, but I edited the code in the example, and it should now serve as valid MVCE. Thanks again for your help. – Rabadash8820 Feb 15 '15 at 18:48
0

Wow, trying to reduce this code to its simplest form for help actually revealed the problem. I had already put braces "{}" after baseUpdates() in AutoScrollMap.h, hence the "function already has body" error. Looks like you sure can call base virtual functions from derived overrides, even if the base function is not public! Sorry for wasting everybody's time, haha.

Rabadash8820
  • 2,328
  • 3
  • 27
  • 49