未定义的引用和非虚拟的 thunk to
undefined reference to and non-virtual thunk to
我有这样的类:
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
的函数,它应该返回布尔值,但没有。
相关文章:
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- C++ Singleton - Prevent ::instance() to variable
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- Visual Studio Code "undefined reference to `WinMain@16'"
- 使用 GCC 卸载的 OpenMP 卸载失败,并出现"Ptx assembly aborted due to errors"
- 如何解决"invalid conversion from 'char' to 'const char*'"
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- Python str to C++ to Python str
- 为什么创建友元类的实例会导致"undefined reference to"错误?
- System.InvalidCastException - SQL to C++ - safe_cast<float>
- 使用 cmake 的 Linux 终端上的"Conversion to non-scalar type is requested"错误
- "no matching function for call to 'Vector::Vector'"错误
- vector<vector<double>> to mxArray using memcpy
- Tensorflow c++ api undefined reference to 'tflite::D efaultErrorReporter()'
- 当覆盖存在时调用基本虚拟"binded to object"函数
- 如何进行故障排除:未定义对"非虚拟 thunk to ..."的引用
- 未定义的引用和非虚拟的 thunk to
- "non-virtual thunk to <method name>" ,引用自:Vtable <classname>for in <objectfile.o>