Unimplemented Pure Virtual Method?
Asked Answered
H

2

6

Here is the problem: I keep getting the unimplemented pure virtual method error when trying to compile. I have implemented all of the pure virtual methods in the abstract base class. Any ideas?

here is the abstract base class:

class record{
public:
    virtual int getID()=0;
    virtual record *clone(); 
};

and the implementation:

class sdata: public record{
public:
    sdata(std::string s = ""){data=s; ID=atoi(data.substr(0,8).c_str());}
    virtual int getID(){return ID;}
private:
    std::string data;
    int ID;
};

sorry, here is the complete error message:

Unimplemented pure virtual method 'getID' in 'record'

Perhaps this bit of code is causing the error then:

int hashTable::hash(record *x) {
   return floor(m * (x->getID() * A - floor(x->getID() * A)));
}
Hothouse answered 3/4, 2013 at 0:51 Comment(12)
Not without seeing the relevant code...Outshine
Don't forget the consts. But yeah, seeing the code might be able to help a little bit.Stayathome
@SongWang: See e.g. #2609799.Outshine
Does class record have an implementation of clone()? Perhaps your compiler considers it to be pure virtual if no implementation exists anywhere in the hierarchy.Artificiality
"unimplemented pure virtual method error" is an oxymoron. You'll need to quote the exact text of the error message.Mhd
You should probably show the code that produces the error. Also add override to the declaration of getID in the derived class and see what happens.Strive
From the error message it sounds like you're trying to create a class of type record instead of data. Could you post the code where you use the class?Leatri
@MattPhillips override is non-standard, though.Leatri
record *temp = new sdata(recs);Hothouse
@Leatri What do you mean? It's part of C++11.Strive
I'm using C++11 and override is not defined..Hothouse
@MattPhillips: It is indeed. Getting a bit daft in my old age. :(Leatri
C
4

Without seeing the code causing the error, it's difficult to know exactly what's going on. If this is a compile-time error, I don't see anything here that would cause it.

However, if you're seeing a runtime error, the two most common causes of this that I can think of are:

(1) Calling the member function from within the base class's constructor or destructor (even indirectly).

(2) The derived class calling the base class's version of the function without it being implemented.

An example showing both of these errors would be:

struct Base {
    Base()
    {
        call_foo(); // Oops, indirectly calls Base::foo() (Scenario 1)
    }
    void call_foo() const {
        foo();
    }
protected:
    virtual void foo() const = 0;
};

struct Derived : Base { protected: virtual void foo() const { Base::foo(); // Oops, unimplemented virtual base function (Scenario 2) } };

int main() { Derived().call_foo(); }

== UPDATE: Possible compile-time error ==

I observe in your example code that record has a non-pure-virtual clone() member function returning a record *. Since record is abstract, you can't create a record directly (only its concrete subclasses). This suggests that your clone() member function should probably also be pure virtual; if it tries to (for example) return new record(), you will get an error that your base class has pure virtual functions.

Cassady answered 3/4, 2013 at 1:19 Comment(4)
Okay I see what you mean... perhaps this bit of code is causing the error then: int hashTable::hash(record *x){ return floor(m * (x->getID() * A - floor(x->getID() * A))); }Hothouse
@ifiamnotmethenwhothehellami: It could be; but it depends on what you're actually passing as the parameter.Leatri
@ifiamnotmethenwhothehellami: If that code is being called by or from within record's constructor (directly or indirectly), that would cause the error. If it's being called after sdata's constructor starts, but before sdata's destructor finishes, it shouldn't cause a problem.Cassady
I had the exact same issue and turns out I had an std::vector of my virtual object, instead of pointers. The downside is that now I have to delete all pointers afterwards.Premiership
D
4

It sounds like you have not implemented all the functions from the abstract base class. If a function in your base has the signature:

    void SomeFuction() const;

And you implement the following in your derived class:

    void SomeFuction();

Then you have have not implemented the function because you omitted the const. GCC should tell you what you did not implement.

Related: if you had a concrete implementation of the base, you would have hidden its name in the derived class. To find functions you are [accidentally] hiding, use -Woverloaded-virtual.

Depend answered 3/4, 2013 at 1:4 Comment(0)
C
4

Without seeing the code causing the error, it's difficult to know exactly what's going on. If this is a compile-time error, I don't see anything here that would cause it.

However, if you're seeing a runtime error, the two most common causes of this that I can think of are:

(1) Calling the member function from within the base class's constructor or destructor (even indirectly).

(2) The derived class calling the base class's version of the function without it being implemented.

An example showing both of these errors would be:

struct Base {
    Base()
    {
        call_foo(); // Oops, indirectly calls Base::foo() (Scenario 1)
    }
    void call_foo() const {
        foo();
    }
protected:
    virtual void foo() const = 0;
};

struct Derived : Base { protected: virtual void foo() const { Base::foo(); // Oops, unimplemented virtual base function (Scenario 2) } };

int main() { Derived().call_foo(); }

== UPDATE: Possible compile-time error ==

I observe in your example code that record has a non-pure-virtual clone() member function returning a record *. Since record is abstract, you can't create a record directly (only its concrete subclasses). This suggests that your clone() member function should probably also be pure virtual; if it tries to (for example) return new record(), you will get an error that your base class has pure virtual functions.

Cassady answered 3/4, 2013 at 1:19 Comment(4)
Okay I see what you mean... perhaps this bit of code is causing the error then: int hashTable::hash(record *x){ return floor(m * (x->getID() * A - floor(x->getID() * A))); }Hothouse
@ifiamnotmethenwhothehellami: It could be; but it depends on what you're actually passing as the parameter.Leatri
@ifiamnotmethenwhothehellami: If that code is being called by or from within record's constructor (directly or indirectly), that would cause the error. If it's being called after sdata's constructor starts, but before sdata's destructor finishes, it shouldn't cause a problem.Cassady
I had the exact same issue and turns out I had an std::vector of my virtual object, instead of pointers. The downside is that now I have to delete all pointers afterwards.Premiership

© 2022 - 2024 — McMap. All rights reserved.