gpt4 book ai didi

c++ - Qt: undefined reference

转载 作者:行者123 更新时间:2023-11-28 02:49:31 25 4
gpt4 key购买 nike

刚开始使用 Qt 遇到一个错误,想知道是否有人可以阐明这个问题。用谷歌搜索并查看了类似的问题,但似乎无法找到解决方案;

C:\Users\Seb\Desktop\SDIcw2\main.cpp:10: error: undefined reference to `SDI::shipHandler::shipHandler(SDI::shipHandler&)'

出现在第 10 行,“w.populateCombo(shipHandler);”在我的 main.cpp 中;

#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()
{
}

船处理程序.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

感谢任何帮助

最佳答案

只是阅读错误消息,它看起来像是在尝试使用复制构造函数 (shippHandler(SDI::shiphandler& tbhCopied)),但您从未在 shippHandler.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);

这些可能是有用的引用:

What is the difference between a definition and a declaration?

c++ passing arguments by reference and pointer

关于c++ - Qt: undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23374653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com