Error LNK2019: unresolved external symbol
-
I've been through 20 subject about this error, but I didn't figure how to fix it
-I'm using Qt 5.0.1, Qtcreatro 2.6.2 - with visual studio 2010 on windows 7 64bit
Here is my code
the .pro file
@#-------------------------------------------------Project created by QtCreator 2013-02-02T23:22:32
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = first
TEMPLATE = app
SOURCES += main.cpp
mycalculator.cpp
display.cppHEADERS += mycalculator.h
display.hOTHER_FILES +=
Comments.txt
@*The .h file *
@#ifndef MYCALCULATOR_H
#define MYCALCULATOR_H
#include <QPushButton>
#include <QTextEdit>
#include "display.h"
class MyCalculator : public QWidget
{
Q_OBJECT
public:
MyCalculator(QWidget *parent = 0);
//QSize sizeHint() const;
private:
Display *mydisplay;
enum{digitArrayIndex = 10};
QPushButton *digitButtons[digitArrayIndex];
QPushButton *addButton,*subButton,*mulButton,*divButton;
//there is 6 functions for now
QPushButton *sinButton,*cosButton,*tanButton;
QPushButton *powerButton,*sqrtButton,*lnButton;
QPushButton *equalButton,*pointButton,*clearButton,*delButton,*ansButton;
private slots:
void digitClicked();
void operationClicked();
void functionClicked();
void equalClicked();
void pointClicked();
void clearClicked();
void delClicked();
void ansClicked();
private: //functions
QPushButton *createButton(const QString &text, const char *buttonSlot);
};
#endif // MYCALCULATOR_H@*The .cpp file *
@#include <QGridLayout>
#include <QScrollArea>
#include "mycalculator.h"
#include "display.h"
MyCalculator::MyCalculator(QWidget *parent)
: QWidget(parent)
{
QGridLayout *myBasicGrid = new QGridLayout(this);
myBasicGrid->setSizeConstraint(QLayout::SetFixedSize);
mydisplay = new Display;
//Implement operation buttons
addButton = createButton("+",SLOT(operationClicked()));
subButton = createButton("-",SLOT(operationClicked()));
mulButton = createButton("\303\227",SLOT(operationClicked()));
divButton = createButton("\303\267",SLOT(operationClicked()));
//Implement function buttons
sinButton = createButton("sin",SLOT(functionClicked()));
cosButton = createButton("cos",SLOT(functionClicked()));
tanButton = createButton("tan",SLOT(functionClicked()));
powerButton = createButton("x\302\262",SLOT(functionClicked()));
sqrtButton = createButton("\342\210\232",SLOT(functionClicked()));
lnButton = createButton("ln",SLOT(functionClicked()));
//The rest of the buttons
equalButton = createButton("=",SLOT(equalClicked()));
pointButton = createButton(".",SLOT(pointClicked()));
ansButton = createButton("Ans",SLOT(ansClicked()));
delButton = createButton("DEL",SLOT(delClicked()));
clearButton = createButton("AC",SLOT(clearClicked()));
//The digit buttons
digitButtons[0] = createButton("0",SLOT(digitClicked()));
for(int i = 1; i<digitArrayIndex; i++){
digitButtons[i] = createButton(QString::number(i),SLOT(digitClicked()));
int row = ((9 - i) / 3) + 2;
int column = ((i - 1) % 3);
myBasicGrid->addWidget(digitButtons[i],row,column); //Adding the digit buttons to the grid
}//layout the display and the rest of the buttons //myBasicGrid->addWidget(mydisplay,0,0,1,5); myBasicGrid->addWidget(digitButtons[0],5,0); myBasicGrid->addWidget(sinButton,1,0); myBasicGrid->addWidget(cosButton,1,1); myBasicGrid->addWidget(tanButton,1,2); myBasicGrid->addWidget(powerButton,1,3); myBasicGrid->addWidget(sqrtButton,1,4); myBasicGrid->addWidget(delButton,2,3); myBasicGrid->addWidget(clearButton,2,4); myBasicGrid->addWidget(mulButton,3,3); myBasicGrid->addWidget(divButton,3,4); myBasicGrid->addWidget(addButton,4,3); myBasicGrid->addWidget(subButton,4,4); myBasicGrid->addWidget(pointButton,5,1); myBasicGrid->addWidget(lnButton,5,2); myBasicGrid->addWidget(ansButton,5,3); myBasicGrid->addWidget(equalButton,5,4);
}
//Slots implemetation
void MyCalculator::digitClicked()
{
}
void MyCalculator::operationClicked()
{
}
void MyCalculator::functionClicked()
{
}
void MyCalculator::equalClicked()
{
}
void MyCalculator::pointClicked()
{
}void MyCalculator::clearClicked()
{
}
void MyCalculator::delClicked()
{
}
void MyCalculator::ansClicked()
{
}
//functions implimintation
QPushButton *MyCalculator::createButton(const QString &text, const char *buttonSlot){
QPushButton *button = new QPushButton(text);
connect(button, SIGNAL(clicked()), this, buttonSlot);
return button;
}@*and the display.h *file // another class I created and Inherited form QTextEdit
@#ifndef DISPLAY_H
#define DISPLAY_H#include <QTextEdit>
class Display : public QTextEdit
{
Q_OBJECT
public:
explicit Display(QWidget *parent = 0);
};
#endif // DISPLAY_H@*the display.cpp file *
@#include "display.h"
Display::Display(QWidget *parent) :
QTextEdit(parent)
{
}@And here is the error
@ : error LNK2019: unresolved external symbol "public: __thiscall Display::Display(class QWidget *)" (??0Display@@QAE@PAVQWidget@@@Z) referenced in function "public: __thiscall MyCalculator::MyCalculator(class QWidget *)" (??0MyCalculator@@QAE@PAVQWidget@@@Z)
debug\first.exe : fatal error LNK1120: 1 unresolved externals
jom: C:\Users\FBM\Desktop\Calculator\first-build-Desktop_Qt_5_0_1_MSVC2010_32bit-debug\Makefile.Debug [debug\first.exe] Error 1120@
When I comment the code in line 13 of the .cpp file the program works fine.
Any help plz -
You should include QFont:
@#include <QFont>@
In addition, in the .cpp file I cannot see when you create the "display" object:
@//display->setReadOnly(true);
/QFont displayFont = display->font();
displayFont.setPointSize(displayFont.pointSize() + 4);
display->setFont(displayFont);/
//Implement operation buttons@ -
[quote author="b1gsnak3" date="1360246401"]Well... I don't see the #include <QWidget> anywhere in your headers[/quote]
This won't be required as it's part of the inheritance chain.
-
I have tried to compile your program and it runs well for me. May be you could re-generate the Makefile and then clean project and re-compile again?
When I had these problems I had some methods declared but not implemented. Can ensure yourself that all the code posted is that you have?
We will solve this issue!
-
What usually works for me is to through away the entire build directory that Qt creates when debugging or running a program. This will remove all files generated by QMake (also your ui files). Then rebuild the program. Problem might be fixed then.
greetz -
[quote author="utcenter" date="1360368863"]I often get such errors, 99.99% of the time it is solved by clicking "clean all" then "run qmake" then "rebuild all".[/quote]
The 0.01% remaining could be missing the name space in the .cpp file. For example:
You define a function in your class header (MyClass.h) :
@ int myFunction(int a, int b);@
But in the MyClass.cpp you miss the namespace:
@int myFunction(int a, int b){
// ...
}@instead of
@int Class::myFunction(int a, int b){
// ...
}@When calling that "myFunction" in your code, the error appears.