Unimplemented Pure Virtual Method?
Solution 1
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.
Solution 2
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
.
mlclmss
Updated on April 05, 2020Comments
-
mlclmss about 4 years
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))); }
-
Oliver Charlesworth about 11 yearsNot without seeing the relevant code...
-
Nbr44 about 11 yearsDon't forget the consts. But yeah, seeing the code might be able to help a little bit.
-
Oliver Charlesworth about 11 years@SongWang: See e.g. stackoverflow.com/questions/2609299/….
-
Chris Hayes about 11 yearsDoes class record have an implementation of clone()? Perhaps your compiler considers it to be pure virtual if no implementation exists anywhere in the hierarchy.
-
user1703401 about 11 years"unimplemented pure virtual method error" is an oxymoron. You'll need to quote the exact text of the error message.
-
Matt Phillips about 11 yearsYou should probably show the code that produces the error. Also add
override
to the declaration ofgetID
in the derived class and see what happens. -
Infiltrator about 11 yearsFrom the error message it sounds like you're trying to create a class of type
record
instead ofdata
. Could you post the code where you use the class? -
Infiltrator about 11 years@MattPhillips
override
is non-standard, though. -
mlclmss about 11 yearsrecord *temp = new sdata(recs);
-
Matt Phillips about 11 years@Infiltrator What do you mean? It's part of C++11.
-
mlclmss about 11 yearsI'm using C++11 and override is not defined..
-
Infiltrator about 11 years@MattPhillips: It is indeed. Getting a bit daft in my old age. :(
-
-
mlclmss about 11 yearsOkay 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))); }
-
Infiltrator about 11 years@ifiamnotmethenwhothehellami: It could be; but it depends on what you're actually passing as the parameter.
-
Adam H. Peterson about 11 years@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.
-
Yvens Rebouças Serpa over 5 yearsI 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.