8

我有这样的课程:

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

这段代码有什么问题?

4

3 回答 3

15

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

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

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

于 2013-02-04T14:43:48.963 回答
2

第一个问题:

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,它应该返回一个布尔值,但没有。

于 2013-02-04T14:52:19.620 回答
1

void setPrice(double price) -> void CD::setPrice(double price)

getPrice() 相同

于 2013-02-04T14:33:29.270 回答