【发布时间】:2015-10-30 18:32:56
【问题描述】:
我正在尝试运行此代码,但我得到的只是链接器错误,而且我真的不知道该做什么或我在这里做错了什么。现在已经为此苦苦挣扎了太久,非常感谢任何帮助,以便我可以继续进行。这也是我的第一个 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
【问题讨论】:
-
从头函数定义中删除
Hasher::以开始。 -
@William_Wilson,虽然这是一个错误,但不会导致链接器问题。
-
@William_Wilson 做到了,没有帮助。
-
@Mpak91 上面的代码无法编译。所以,奇怪的是你有链接器错误。可能生成文件和目标文件没有更新。您可以尝试:清理、运行 qmake 和构建。您应该会看到编译器错误。
-
无论如何,您应该决定是从
QCryptographicHash继承还是将其用作私有成员。如果它是私人成员,它应该首先被证明。如果您将其子类化,则不需要QCryptographicHash *QCryptObject;