刚开始使用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()
{
}
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);
这些可能是有用的参考:
What is the difference between a definition and a declaration?
c++ passing arguments by reference and pointer