未定义的引用和非虚拟的 thunk to

undefined reference to and non-virtual thunk to

本文关键字:thunk to 虚拟 引用 未定义      更新时间:2023-10-16

我有这样的类:

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;
    }

我有这些错误:

/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 ===|

这段代码有什么问题?

除了momogentoo提到的缺少CD::限定符错误外,这是另一个非常狡猾的错误:

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

由于您使用了单个冒号,因此它不会被解释为解析运算符,而是解释为标签(对于 gotos)。该语句实际上所做的只是对同一字符串对象的自赋值(对于 std::string 将不起作用)。

void setPrice(双倍价格) -> void CD::setPrice(双倍价格)

与获取价格()相同

第一个问题:

undefined reference to `CD::getPrice()'

该函数的定义缺少CD::限定;因此它声明并定义了一个非成员函数。

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

同样对于CD::setPrice.

第二个问题:

undefined reference to `typeinfo for  MusicProduct'

据推测,MusicProduct应该是一个抽象类,您不想为其虚函数提供定义。在这种情况下,您必须声明它们纯虚拟

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

如果它不应该是抽象的,那么你需要实现这些功能。

第三个问题:

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

据推测,您有一个名为 MusicStore::hasProduct 的函数,它应该返回布尔值,但没有。