PDA

View Full Version : error: LNK1120: 1 unresolved externals



Mathan
13th September 2016, 15:31
Hi,
i am using Qt 5.7

I want to make utilize the other class functions.But I am getting the error

searchctlr.obj:-1: error: LNK2019: unresolved external symbol "public: __cdecl utilityCtlr::utilityCtlr(void)" (??0utilityCtlr@@QEAA@XZ) referenced in function "public: void __cdecl SearchCtlr::writeXML(class QString,class QString)" (?writeXML@SearchCtlr@@QEAAXVQString@@0@Z)

Code Snippet:

This utility class for generic operation which is common to whole project. Ex: encryption/decryption etc.,

utility.h
--------
#ifndef UTILITY_H
#define UTILITY_H

#include <QObject>

class utilityCtlr : public QObject
{

public:

utilityCtlr();

void utilEncryption();

};
#endif // UTILITY_H

utility.cpp
-----------
#include "utility.h"
#include <QDebug>

utilityCtlr::utilityCtlr()
{


}

void utilityCtlr::utilEncryption()
{

QString strUserName = name;

}

The class calling the utility:
searchctrl.h
-------------

#ifndef SEARCHCTLR_H
#define SEARCHCTLR_H


#include <QObject>
//#include "search.h"

#include "utility.h"



class SearchCtlr : public QObject
{
Q_OBJECT

public:

explicit SearchCtlr(QObject *parent = 0);
Q_INVOKABLE void doSearch(QString name);

private:
utilityCtlr *m_utilityCtlr;

};

searchctrl.cpp
---------------

#include "searchctlr.h"
#include <QDebug>

#include <QDomDocument>
#include <QFile>
#include <QDir>
#include<QXmlStreamReader>
#include<QXmlStreamWriter>

SearchCtlr::SearchCtlr(QObject *parent)
{
// m_search = new Search();
// qDebug()<<__FILE__<<__LINE__<<__FUNCTION__<<"SearchCtlr Constructor******";
}



void SearchCtlr::doSearch(QString name)
{

m_utilityCtlr = new utilityCtlr(); //Error appears when this two lines are uncommented.
m_utilityCtlr->utilEncryption(name);

}




















#endif // SEARCHCTLR_H

d_stranz
13th September 2016, 17:04
Looks like you haven't added utility.cpp to your project. You have #include-ed utility.h into searchCtrl.h, so the compiler "knows" there is a constructor for the class utilCtlr, but if you don't link in the utility.obj file created from utility.cpp, the linker can't find the code for it.

Mathan
14th September 2016, 08:55
Hi d_stranz,
Thanks for the reply.

but if you don't link in the utility.obj file created from utility.cpp

could you please tell me how to do it?
The method i followed, Added the utility.h/utility.cpp in the project through AddNew. #include "utility.h" in the SearchCtrl.H and in utility.cpp. Am I missing some steps?

d_stranz
15th September 2016, 15:56
Ah, I didn't notice that you had derived the utilCtlr class from QObject. You are missing the Q_OBJECT macro from the class definition:



class utilityCtlr : public QObject
{
Q_OBJECT // <<<<<
public:

utilityCtlr();

//...

};


If you aren't using any QObject features in your class (signals, slots, etc.) than it doesn't make much sense to derive from QObject. There is no requirement to derive C++ classes from a Qt base class in order to use the class in a Qt project.

If this is your actual code, I think you should be getting many more link errors than just the one you show because of the missing Q_OBJECT macro.

Mathan
15th September 2016, 17:21
Hi,
I added the QObject, still i face the errors. Simply I create QtCPPV1.h/QtCPPV1.cpp from these files I want to access the funtions of utility.h/utility.cpp. When I try to create the object of the class I face the error.
Code snippet:

QtCPPV1.h
------------

#include <QQuickItem>

#include "utility.h"

class QtCPPV1 : public QQuickItem
{
Q_OBJECT

public:
QtCPPV1(QQuickItem* parent = 0);
~QtCPPV1();

Q_INVOKABLE void ss();

private:

utility* m_utility;

};

#endif // QTQCPPXMLINTEGRATIONV1_H

QtCPPV1.cpp
----------------

#include "QtCPPV1.h"
#include <QDebug>

QtCPPV1::QtCPPV1(QQuickItem* parent) :
QQuickItem(parent)
{
m_utility = new utility();//Error appears here
}

QtCPPV1::~QtCPPV1()
{
}


void QtCPPV1::ss()
{

m_utility->delEncryption();//Error appears here
}


utility.h
----------
#ifndef UTILITY_H
#define UTILITY_H



#include <QXmlStreamWriter>
#include <QByteArray>

class utility : public QXmlStreamWriter
{


public:

utility();
~utility();

void delEncryption();


};




#endif // UTILITY_H

utility.cpp
----------
#include "utility.h"
#include <QDebug>
#include <QFile>



utility::utility()
{


}

utility::~utility()
{


}

void utility::delEncryption()
{


}

d_stranz
15th September 2016, 17:38
Please do not just copy and paste your code into a post. It is unreadable. As I say in my sig (see below), use CODE tags. The sig even tells you how to do it.

I don't know if you are using Qt Creator or Visual Studio to manage and build your project. If you are using Qt Creator, then you probably have not run qmake since you added (or renamed) the files in your project. If you are using Visual Studio and have correctly added all of the source files you use to your project then a "rebuild" or "clean" followed by "build" should fix this.

There is nothing obviously wrong with your source code. The problem is almost certainly as I said earlier: utility.cpp is not being compiled and/or utility.obj is not being linked into your final binary.

Mathan
16th September 2016, 08:23
Hi d_stranz,

Run the Qmake and built the project as you said. Its working now. This error cost me 3hrs.

Thanks a lot.