Undefined reference to and not virtual

I have classes like this:

class Product { public : virtual double getPrice(); virtual void setPrice(double price); }; class MusicProduct { protected: string author; double price; public : virtual string getAuthor(); virtual void setAuthor(string author); ~MusicProduct(); }; class CD : public MusicProduct, public Product { public : string getAuthor(); void setAuthor(string author); double getPrice(); void setPrice(double price); }; string CD::getAuthor() { return MusicProduct::author; } void CD::setAuthor(string author) { MusicProduct:author = author; } void setPrice(double price) { MusicProduct::price = price; } double getPrice() { return MusicProduct::price; } 

And I have these errors:

 /home/katie/Desktop/Temp/MusicStore.cpp||In member function 'virtual bool MusicStore::hasProduct( Product)':| /home/katie/Desktop/Temp/MusicStore.cpp|15|warning: no return statement in function returning non-void [-Wreturn-type]| /home/katie/Desktop/Temp/MusicStore.cpp||In member function 'virtual Product MusicStore::getProduct( Product)':| /home/katie/Desktop/Temp/MusicStore.cpp|20|warning: no return statement in function returning non-void [-Wreturn-type]| /home/katie/Desktop/Temp/MusicStore.cpp||In member function 'virtual bool MusicStore::buyProduct( Product)':| /home/katie/Desktop/Temp/MusicStore.cpp|25|warning: no return statement in function returning non-void [-Wreturn-type]| /home/katie/Desktop/Temp/MusicStore.cpp||In member function 'virtual bool MusicStore::returnProduct( Product)':| /home/katie/Desktop/Temp/MusicStore.cpp|30|warning: no return statement in function returning non-void [-Wreturn-type]| /home/katie/Desktop/Temp/Store/CD.cpp||In member function 'virtual void CD::setAuthor(std::string)':| /home/katie/Desktop/Temp/Store/CD.cpp|12|warning: label 'MusicProduct' defined but not used [-Wunused-label]| obj/Debug/Store/CD.o:(.rodata._ZTVN5Music2CDE[vtable for CD]+0x10)||undefined reference to ` CD::getPrice()'| obj/Debug/Store/CD.o:(.rodata._ZTVN5Music2CDE[vtable for CD]+0x14)||undefined reference to ` CD::setPrice(double)'| obj/Debug/Store/CD.o:(.rodata._ZTVN5Music2CDE[vtable for CD]+0x20)||undefined reference to `non-virtual thunk to CD::getPrice()'| obj/Debug/Store/CD.o:(.rodata._ZTVN5Music2CDE[vtable for CD]+0x24)||undefined reference to `non-virtual thunk to CD::setPrice(double)'| obj/Debug/Store/CD.o:(.rodata._ZTIN5Music2CDE[typeinfo for CD]+0x10)||undefined reference to `typeinfo for MusicProduct'| obj/Debug/Store/CD.o:(.rodata._ZTIN5Music2CDE[typeinfo for CD]+0x18)||undefined reference to `typeinfo for Product'| ||=== Build finished: 6 errors, 5 warnings ===| 

What is wrong with this code?

+4
source share
3 answers

Besides the missing CD :: qualifier error mentioned by momogentoo, this is another very impenetrable error:

 void CD::setAuthor(string author) { MusicProduct:author = author; // <-- !!! } 

Since you used a single colon, it is not interpreted as a permission statement, but as a label (for gotos). What the instruction actually does is simply self-determination for the same string object (which for std :: string will have no effect).

+8
source

First problem:

 undefined reference to `CD::getPrice()' 

Your definition of this function does not qualify CD:: ; therefore, it instead declares and defines a function that is not a member.

 double CD::getPrice() {// ^^^^ Add this MusicProduct::price = price; } 

Similarly for CD::setPrice .

The second problem:

 undefined reference to `typeinfo for MusicProduct' 

Presumably, MusicProduct should be an abstract class, and you don't want to provide definitions for your virtual functions. In this case, you should declare them pure virtual:

 virtual double getPrice() = 0; // ^^^ Add this 

If it should not be abstract, you need to implement these functions.

The third problem:

 In member function 'virtual bool MusicStore::hasProduct( Product)': warning: no return statement in function returning non-void [-Wreturn-type] 

Presumably, you have a function called MusicStore::hasProduct , which should return a boolean value, but it does not.

+2
source

void setPrice (double price) → void CD :: setPrice (double price)

same for getPrice ()

+1
source

All Articles