我有这样的课程:
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 ===|
此代码有什么问题?
答案 0 :(得分:10)
除了momogentoo提到的缺少CD :: qualifier错误之外,这是另一个非常偷偷摸摸的错误:
void CD::setAuthor(string author)
{
MusicProduct:author = author; // <-- !!!
}
由于您使用了单个冒号,因此它不会被解释为解析运算符,而是作为标签(对于gotos)。该语句实际上只是为同一个字符串对象进行自我赋值(对于std :: string将没有效果)。
答案 1 :(得分: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
的函数,它应该返回一个布尔值,但不会。
答案 2 :(得分:1)
void setPrice(double price) - &gt; void CD :: setPrice(double price)
getPrice()
相同