-2

I have an abstract base class and its derived class, although I implemented the pure functions in the cpp file of the derived class, when I try to create an object of the derived I still get an error that says that the derived class is an abstract!! Base:

class Base  {
protected:
    string name;
    int quantity;
    double price;
public:
    c'tor....
    virtual ~Base(){}
    virtual void buy(int num) = 0;
    virtual Base& operator+(const Base& b)=0;
    };

Derived:

#include "Base.h"

class Derived : public Base{
protected:
    double percentage;
public:
    c'tor...
    virtual ~Derived() {}
    virtual void buy(int num);
    virtual Derived& operator+(const Derived& d);

};

Derived.cpp:

#include "Derived.h"
void Derived::buy(int num){
   //implementation
}
Derived& Derived::operator+(const Derived& d){
   //implemetation
}

main.cpp:

#include "Base.h"
#include "Derived.h"
int main()   {
    Derived d;
    //...
}

Error:

1   IntelliSense: object of abstract class type "Derived" is not allowed:
            pure virtual function "Base::buy" has no overrider  c:\Users\aub\Documents\Visual Studio 2013\Projects\Project22\Project22\main.cpp 10
stringson
  • 21
  • 1
  • 6
  • 1
    Make sure to save and recompile. `IntelliSense` tends to fool you about such stuff until your code was completely reindexed again. – πάντα ῥεῖ Jun 10 '16 at 18:20
  • You can specify `overrride` after your derived virtual function and the compiler may give you more guidance to where the issue is – kmdreko Jun 10 '16 at 18:30
  • Try using the `override` keyword after `virtual void buy(int num)` just in case... Who knows, maybe some macro replaces `int` with something else? – KABoissonneault Jun 10 '16 at 18:30
  • @perencia Consider consulting the [new ghost busters](http://www.imdb.com/title/tt1289401/). – πάντα ῥεῖ Jun 10 '16 at 18:31
  • If it is not, in-fact, a build issue then show us the constructors of `Derived` and `Base` - could be an issue there. – sjrowlinson Jun 10 '16 at 18:35
  • I can't make this fail. g++ Derived.cpp main.cpp -o main links and executes fine – systemcpro Jun 10 '16 at 18:37
  • `Derived& Derived::operator+(const Derived& d)` has a different return type than the `operator+` in the base class, so it does not override the abstract function in the base. – kfsone Jun 10 '16 at 23:19
  • @kfsone but I was taught that return type can only be differenet in case which the virtual function returns a pointer or a reference to the base class.. – stringson Jun 10 '16 at 23:37
  • @stringson see http://ideone.com/y7VAuq and http://ideone.com/7jSwQt – kfsone Jun 10 '16 at 23:46
  • The method you added doesn't properly override the pure virtual in the base, because the parameter differs. Covariant return values are supported in C++ virtual overriding; covariant parameters are not. See this question: http://stackoverflow.com/questions/11821158/c-covariance-in-parameters Think about it: the `Derived` version of `+` cannot insist that the argument is a `Derived`, because it can be called through the base class virtual which takes **any** `Base` object, whether or not it is `Derived`. – Kaz Jun 11 '16 at 00:04
  • @kfsone C++ virtual overriding does permit covariant return types. But not parameters. If a base virtual returns `Base *`, then a derived is allowed to return `Derived *`. A `Derived` is a `Base`, so nothing is violated. If the `Derived*` to `Base*` conversion is non-trivial (pointers have to be displaced or whatever) the C++ compiler will emit the thunk code or whatever, and stuff the right things into the vtables to make it work. Arguments are a different story. – Kaz Jun 11 '16 at 00:06

1 Answers1

-4

You have some build problem. Might it be that a stale version of the Derived object file is being linked? Or Derived.cpp is not being compiled and linked into the project at all?

Do a fresh rebuild from scratch.

Kaz
  • 55,781
  • 9
  • 100
  • 149
  • 4
    This is not an answer. It is a Comment/Hint/Question/Wonder – Khalil Khalaf Jun 10 '16 at 18:28
  • @stringson _"I swear I did"._ Show evidence. We are nerds, nitpickers and rely on scientifically reproducible results and test cases. 1st step to achieve attention there is to post a [MCVE]. – πάντα ῥεῖ Jun 10 '16 at 18:41
  • @stringson When you rebuild everything, do you see that `Derived.cpp` is being compiled? Check all the project files. Is the object file corresponding to `Derived.cpp` being linked? – Kaz Jun 10 '16 at 20:32
  • @FirstStep Do you have a better answer? The code *is* in fact correct C++. If you suspect it's a compiler or linker bug rather than a problem with an incomplete build, make that an answer. I don't think it's likely, given the triviality of the test case. – Kaz Jun 10 '16 at 20:35
  • @kaz how can I make those checks? – stringson Jun 10 '16 at 23:05
  • Actually there is one more pure virtual method, I'll edit my question and write it – stringson Jun 10 '16 at 23:07
  • @stingson The most basic sanity check is: make some minor edit to `Derived.cpp` and save it. Then if you do a build, do you see `Derived.cpp` being recompiled in the compiler output window? If not, then your IDE doesn't actually know that `Derived.cpp` is part of your project. – Kaz Jun 10 '16 at 23:17
  • @Kaz so it has nothing to do with the method I added? – stringson Jun 10 '16 at 23:38
  • That said, it's not clear how a problem with the `+` virtual would turn into an error diagnostic about the unrelated `buy` virtual! – Kaz Jun 11 '16 at 00:00