刚开始使用Qt并遇到错误,想知道是否有人可以解决这个问题。谷歌搜索并查看类似的问题,但似乎无法得到解决方案;
C:\Users\Seb\Desktop\SDIcw2\main.cpp:10: error: undefined reference to `SDI::shipHandler::shipHandler(SDI::shipHandler&)'
发生在第10行,&#34> 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()
{
}
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
感谢任何帮助
答案 0 :(得分:2)
只是阅读错误消息,看起来它正在尝试使用复制构造函数(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);
这些可能是有用的参考:
What is the difference between a definition and a declaration?