2015-10-30 42 views
2

我试图运行此代码,但我所得到的是链接器错误,并不真正知道该怎么做或我在做什么错在这里。现在一直在苦苦挣扎,所以我非常感谢任何帮助,这样我就可以做到这一点。这也是我的第一个Qt应用程序。Qt链接器错误窗口

运行Qt Creator的3.5.1,基于Qt 5.5.1(MSVC 2013年,32位) 编译器:微软的Visual C++编译器12.0 操作系统:Windows 8.1专业版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)" ([email protected]@[email protected]@@[email protected]@@@Z) referenced in function main 

main.obj:-1: error: LNK2019: unresolved external symbol "public: __cdecl Hasher::~Hasher(void)" ([email protected]@[email protected]) 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 
+1

从头文件函数定义中移除'Hasher ::'以开始。 –

+0

@William_Wilson,虽然这是一个错误,但不会导致链接器问题。 – SergeyA

+0

@William_Wilson做到了,没有帮助。 – mpak

回答

3

因此,链接器错误是由于未更新生成文件和目标文件导致的,因为新的Hasher.cpp根本没有被压缩。在这种情况下,重建项目可能会有所帮助:Clean,Run qmakeBuild

2

Hasher::Hasher你需要调用基类的构造函数:

Hasher::Hasher(const QByteArray &data, Algorithm method) 
    : QCryptographicHash(method) 
{ 
    QByteArray result = this->hash(data, method); 
    this->output = result.toHex(); 
} 

我不知道为什么MSVC编译代码的话,那甚至不应该得到链接。

+0

当然,它修复了编译错误,但它没有解释链接器错误。 –

+0

@OrestHera我目前无法访问MSVC,但如果它编译引用的代码,我不会感到惊讶。 – Paul

+0

试过这个,还是一样的错误。 (还是)感谢你的建议。 – mpak