Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined reference to and non-virtual thunk to [closed]

I have such classes:

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 those 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?

like image 805
Katie Avatar asked Feb 04 '13 14:02

Katie


1 Answers

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

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

Since you used a single colon, it isn't interpreted as the resolution operator, but as a label (for gotos). What the statement will actually do is just self-assignment for the same string object (which for std::string will have no effect).

like image 183
user1610015 Avatar answered Sep 28 '22 20:09

user1610015