Qt:未定义对的引用
Qt: Undefined reference to
刚开始使用Qt时遇到一个错误,不知道是否有人能澄清这个问题。在谷歌上搜索并查看类似的问题,但似乎无法找到解决方案;
C:UsersSebDesktopSDIcw2main.cpp:10: error: undefined reference to `SDI::shipHandler::shipHandler(SDI::shipHandler&)'
出现在我的main.cpp中的第10行"w.populateCombo(shipHandler(;";
#include "widget.h"
#include <QApplication>
#include "shipHandler.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
SDI::shipHandler shipHandler("ships/ships.txt");
w.populateCombo(shipHandler);
return a.exec();
}
shipHandler.cpp(构造函数和析构函数(
SDI::shipHandler::shipHandler(std::string fileName)
{
shipCount = 0;
std::string line;
std::ifstream infile;
infile.open(fileName.c_str());
while(!infile.eof())
{
getline(infile,line);
shipHandler::lineParse(line);
shipCount++;
}
infile.close();
}
SDI::shipHandler::~shipHandler()
{
}
shipHandler.h
#ifndef SDI__shipHandler
#define SDI__shipHandler
#include "common.h"
#include "navalVessels.h"
namespace SDI
{
class shipHandler
{
//variables
public:
std::vector<SDI::navalVessels*> ships;
int shipCount;
private:
//function
public:
shipHandler();
shipHandler(std::string fileName);
shipHandler(SDI::shipHandler& tbhCopied);
~shipHandler();
void lineParse(std::string str);
void construct(std::vector<std::string> line);
std::vector<int> returnDates(std::string dates);
private:
};
}
#endif
如有任何帮助,我们将不胜感激。
刚读到错误消息,它似乎正在尝试使用复制构造函数(shipHandler(SDI::shipHandler& tbhCopied)
(,但您从未在shipHandler.cpp.中完全定义过它
class shipHandler
{
// ...
public:
shipHandler(); // this isn't defined anywhere
shipHandler(std::string fileName);
shipHandler(SDI::shipHandler& tbhCopied); // this isn't defined anywhere
~shipHandler();
// ...
};
首先,您应该停止声明复制构造函数,或者完成对它的定义:
// in shipHandler.cpp
SDI::shipHandler::shipHandler(SDI::shipHandler& tbhCopied) {
// fill this in
}
您还应该定义或删除默认构造函数(SDI::shipHandler::shipHandler()
(。
接下来,您可能可以将shipHandler
作为参考而不是创建副本:
// most likely, this is what you want
void Widget::populateCombo(const shipHandler& handler);
// or maybe this
void Widget::populateCombo(shipHandler& handler);
这些可能是有用的参考:
定义和声明之间有什么区别?
c++通过引用和指针传递参数
相关文章:
- 编译C++时未定义的引用
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 未定义的引用在哪里
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- 对Py_Initialize()的未定义引用
- 使用mysql c++连接器的未定义引用
- 未定义的引用 .. 使用 OpenCV 编译 C++ 代码时,从命令行
- 对 Scalar ::Scalar() 的未定义引用
- 已定义函数时出现 G++ "未定义的引用"错误
- 链接 cmake 时出现未定义的引用错误
- 对复制 CTOR 和 CTOR 的未定义引用
- 错误:未定义对'oboe::AudioStreamBuilder::openStream(oboe::AudioStream**)'的引用
- 右值引用未定义的行为
- 对"vtable for DigitalClock"的未定义引用 - 未定义对"DigitalClock::staticMetaObject"的引用 - Qt
- 函数类型到函数类型的引用:未定义的引用
- avcodec_alloc_context引用未定义,但ffmpeg链接顺序正确
- 从c++调用Fortran子程序,链接时引用未定义
- 枚举引用未定义,但位于包含的头文件中