我正在尝试运行此代码,但我得到的只是链接器错误,并且不知道该做什么或我在这里做错了什么。现在已经挣扎太久了,任何帮助都非常感激,所以我可以把这件事情推进去。这也是我有史以来的第一个Qt应用程序。
运行Qt Creator 3.5.1,基于Qt 5.5.1(MSVC 2013,32位) 编译器:Microsoft Visual C ++编译器12.0 操作系统:Windows 8.1 Pro 64位
我有Qt项目,我有文件:
Hasher.h
#ifndef HASHER_H
#define HASHER_H
#include <QString>
#include <QCryptographicHash>
class Hasher : public QCryptographicHash
{
public:
Hasher(const QByteArray &data, Algorithm method); /* Constructor */
~Hasher(); /* Destructor */
QString name, output;
private:
QCryptographicHash *QCryptObject;
};
#endif // HASHER_H
Hasher.cpp
#include "hasher.h"
/* Destructor */
Hasher::~Hasher() {
}
/*
* Constructor Hasher(QByteArray &, Algorithm) generates hash
* from given input with given algorithm-method
*/
Hasher::Hasher(const QByteArray &data, Algorithm method) {
QByteArray result = this->QCryptObject->hash(data, method);
this->output = result.toHex();
}
的main.cpp
#include <QCoreApplication>
#include <QString>
#include <QFile>
#include <QDebug>
#include "hasher.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString fileName;
QTextStream stream(stdin);
qDebug() << "MD5 generator!" << endl;
qDebug() << "Give filename to generate checksum from: ";
fileName = stream.readLine();
QFile* file = new QFile(fileName);
if(file->open(QIODevice::ReadOnly))
{
Hasher hasher(file->readAll(), QCryptographicHash::Md5);
qDebug() << "MD5 Hash of " << fileName << " is: " << hasher.output << endl;
file->close();
}
return a.exec();
}
我得到的错误:
main.obj:-1: error: LNK2019: unresolved external symbol "public: __cdecl Hasher::Hasher(class QByteArray const &,enum QCryptographicHash::Algorithm)" (??0Hasher@@QEAA@AEBVQByteArray@@W4Algorithm@QCryptographicHash@@@Z) referenced in function main
main.obj:-1: error: LNK2019: unresolved external symbol "public: __cdecl Hasher::~Hasher(void)" (??1Hasher@@QEAA@XZ) referenced in function main
debug\MD5-generator.exe:-1: error: LNK1120: 2 unresolved externals
.pro文件
QT += core
QT -= gui
TARGET = MD5-generator
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp \
hasher.cpp
HEADERS += \
hasher.h
答案 0 :(得分:3)
因此,链接器错误是由未更新的makefile和目标文件引起的,因为新的Hasher.cpp
根本没有被编译。在这种情况下,重建项目可能有所帮助:Clean
,Run qmake
,Build
。
答案 1 :(得分:2)
在Hasher::Hasher
中,您需要调用基类构造函数:
Hasher::Hasher(const QByteArray &data, Algorithm method)
: QCryptographicHash(method)
{
QByteArray result = this->hash(data, method);
this->output = result.toHex();
}
我不知道为什么MSVC根本编译该代码,它甚至不应该链接。