I am busy with my university assignment, i'm new to Qt, C++ and i am required to:
Define and implement the Address class in separate header and source files
I am getting confused with Qt4 and Qt5 as the prescribed text book gives all examples in Qt4 yet we are required to code with Qt5.
I keep getting errors and the latest error is :
error: undefined reference to
Dialog7getTextEP7QWidgetRK7QStringS4_N9QLineEdit8EchoModeES4_
Pb6QFlagsIN2Qt10WindowTypeEES8_INS9_15InputMethodHintEE'
error: undefined reference to
MessageBox11informationEP7QWidgetRK7QStringS4_6QFlagsINS_
14StandardButtonEES6
collect2.exe:-1: error: error: ld returned 1 exit status
I am very confused and stuck, please if anyone can steer me in the right direction i would greatly appreciate it, This is my code so far:
Header File:
#ifndef ADDRESS_H_
#define ADDRESS_H_
#include <QString>
#include <QFile>
#include <QStringList>
#include <QtCore>
QTextStream cout(stdout);
QTextStream cerr(stderr);
class Address{
public:
Address();
void setLines(QString ad, QString sep);
QString getPostalCode() const;
QString toString(QString sep) const;
static bool isPostalCode(QString pc);
private:
static QStringList lines;
};
#endif // ADDRESS_H_
Main File:
#include "address.h"
#include <iostream>
#include <QFile>
#include <sstream>
#include <fstream>
#include <QStringList>
#include <QString>
#include <QTextStream>
#include <QCoreApplication>
#include <QtWidgets/QInputDialog>
#include <QtWidgets/QMessageBox>
#include <QtWidgets/QLineEdit>
Address::Address(){}
void Address::setLines(QString ad, QString sep){
QStringList lines;
lines = ad.split(sep, QString::SkipEmptyParts);
}
QString Address::getPostalCode() const{
QStringList lines;
return lines.last();
}
QString Address::toString(QString sep) const{
QStringList lines;
return lines.join(sep);
}
bool Address::isPostalCode(QString pc){
if (pc >= "0" && pc <= "9999")
return true;
else
return false;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
while(true)
{
QString userInput = QInputDialog::getText(0,"Address Input","Please
enter your address:");
QLineEdit::EchoMode ok = QLineEdit::Normal;
QString();
if(ok && !userInput.isEmpty())
{
Address ad;
QMessageBox::information(0, "User Address",ad.toString(userInput));
}
}
return 0;
}
I got the same error. Basically, there's an issue with your compiler MingW and qMake. The best suggestion I have is to uninstall QTcreator and everything that references it. And re-install it using the new version: https://www.qt.io/download
This will install everything you need and all the right directories. Hope this helps.
Related
I created a new class and added a function to it which is supposed to retrieve data on the current euro exchange rate. I used QNetworkAccessManager. When I try to build project I got errors. After I comment QNetworkAccessManager logic errors disapeard.
Currencies.h
#ifndef CURRENCIES_H
#define CURRENCIES_H
#include <QObject>
#include <QFile>
#include <QTextStream>
#include <QtDebug>
#include <QList>
#include <QProcess>
#include <QRegularExpression>
#include <QSettings>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QNetworkRequest>
#include <QtXml/QtXml>
#include <QDebug>
#include <string.h>
#include <QFile>
#include <QByteArray>
#include <QJsonObject>
#include <QJsonDocument>
#include <QObject>
#include <algorithm>
#include "Connections.h"
class Euro : public QObject
{
Q_OBJECT
private:
//Ui::MainWindow *ui;
float euroRate=0;
float finalPriceEuro = 0;
float finalPrice = 0;
QString const urlEuroString = "http://api.nbp.pl/api/exchangerates/rates/a/eur/?format=json";
Connection::Status connectStatus;
QSettings settings;
public:
Euro();
~Euro();
float GetEuroRate() const;
void SetEuroRate(double);
void SetFinalPriceEuro(float);
float GetFinalPriceEuro() const;
void SetFinalPrice(float);
float GetFinalPrice() const;
QString GetEuroUrl() const;
void SaveEuroRate();
void LoadEuroRate();
};
#endif // CURRENCIES_H
Currencies.cpp
#include "Currencies.h"
Euro::Euro()
{
QNetworkAccessManager* manager = new QNetworkAccessManager(this);
connect(manager,&QNetworkAccessManager::finished,this,[&](QNetworkReply* reply)
{
if(reply->bytesAvailable())
{
connectStatus.SetConnectionStatus(true);
}
else
{
connectStatus.SetConnectionStatus(false);
}
});
qDebug()<<connectStatus.GetConnectionStatus();
}
Euro::~Euro()
{
}
float Euro::GetEuroRate() const{
return euroRate;
}
void Euro::SetEuroRate(double rateValue){
euroRate = rateValue;
}
QString Euro::GetEuroUrl() const{
return urlEuroString;
}
void Euro::SetFinalPriceEuro(float price)
{
finalPriceEuro = price;
}
float Euro::GetFinalPriceEuro() const
{
return finalPriceEuro;
}
void Euro::SetFinalPrice(float price)
{
finalPrice = price;
}
float Euro::GetFinalPrice() const
{
return finalPrice;
}
void Euro::SaveEuroRate(){
//settings.setValue("euroRate",currencyRate.GetEuroRate());
qDebug()<<"Zapisano!";
}
void Euro::LoadEuroRate()
{
}
Errors
:-1: error: CMakeFiles/ATLAS.dir/Currencies.cpp.obj:Currencies.cpp:(.text+0x2b0): undefined reference to `__imp__ZN21QNetworkAccessManagerC1EP7QObject'
:-1: error: CMakeFiles/ATLAS.dir/Currencies.cpp.obj:Currencies.cpp:(.text+0x2b7): undefined reference to `__imp__ZN21QNetworkAccessManager8finishedEP13QNetworkReply'
:-1: error: CMakeFiles/ATLAS.dir/Currencies.cpp.obj:Currencies.cpp:(.text+0x2d6): undefined reference to `__imp__ZN21QNetworkAccessManager16staticMetaObjectE'
:-1: error: collect2.exe: error: ld returned 1 exit status
Any idea how to solve this? Thanks a lot !
I'm trying to create tests for a c++ application with QtTest. The three relevent files that I have are: GuiTests.cpp which contains my main function, testsuite1.cpp which contains my tests and testsuite1.h which contains the definitions of my tests. I created these files with help from different guides, for example this one.
When I try to build I get this error:
no matching function for call to 'qExec(TestSuite1 (*)(), int&, char**&)'
no known conversion for argument 1 from 'TestSuite1 (*)()' to 'QObject*'
I don't understand why, as you can see in testsuite.h below TestSuite1 is a QObject. The funny thing is this exact code (I am pretty sure) worked before but then I fiddled around with passing argc and argv to guiTest() for a while, and after I removed argc and argv and went back to what I had before (what I currently have, please see the files below) I got this error.
I've been trying to solve this problem for a long time and I can't find any answers online, so please help me, any help is appreciated. Thanks!
GuiTests.cpp
#include "testsuite1.h"
#include <QtTest>
#include <QCoreApplication>
int main(int argc, char** argv) {
TestSuite1 testSuite1();
return QTest::qExec(&testSuite1, argc, argv);
}
testsuite1.h
#ifndef TESTSUIT1_H
#define TESTSUIT1_H
#include <QMainWindow>
#include <QObject>
#include <QWidget>
#include <QtTest>
class TestSuite1 : public QObject {
Q_OBJECT
public:
TestSuite1();
~TestSuite1();
private slots:
// functions executed by QtTest before and after test suite
void initTestCase();
void cleanupTestCase();
// functions executed by QtTest before and after each test
//void init();
//void cleanup();
// test functions
void testSomething();
void guiTest();
};
#endif // TESTSUIT1_H
testsuite1.cpp
#include "testsuite1.h"
#include <QtWidgets>
#include <QtCore>
#include <QtTest>
TestSuite1::TestSuite1()
{
}
TestSuite1::~TestSuite1()
{
}
void TestSuite1::initTestCase()
{
}
void TestSuite1::cleanupTestCase()
{
}
void TestSuite1::guiTest()
{
QVERIFY(1+1 == 2);
}
void TestSuite1::testSomething()
{
QLineEdit lineEdit;
QTest::keyClicks(&lineEdit, "hello world");
QCOMPARE(lineEdit.text(), QString("hello world"));
//QVERIFY(1+1 == 2);
}
//QTEST_MAIN(TestSuite1)
//#include "TestSuite1.moc"
TestSuite1 testSuite1();
declares a function named testSuite1 returning TestSuite1. Taking address of it gives you TestSuite1 (*)() (a function pointer) instead of TestSuite1* that would convert to QObject*.
Use one of the following:
TestSuite1 testSuite1;
TestSuite1 testSuite1{};
auto testSuite1 = TestSuite();
auto testSuite1 = TestSuite{};
to declare a variable.
I try to add two QStrings into one. I've read a lot about:
QString NAME = QString + QString
but this doesn't helps me here. Thats how my code look so far:
test.h
#ifndef TEST_H
#define TEST_H
#include <QString>
#include <QFile>
#include <QDir>
class Test
{
public:
void createProject(QString* p, QString*n);
};
#endif // TEST_H
test.cpp
#include "test.h"
#include <QFile>
#include <QString>
#include <QDir>
void Test::createProject(QString *p, QString *n)
{
QString result = p + n;
QDir dir(result);
if (dir.exists())
{
// ok
}
else
{
printf("Error!\n");
}
}
(ignore the code about checking if the directory exists, btw I use Qt 4.8.6)
So now when I try to compile, I get this error:
test.cpp: In member function 'void Test::createProject(QString*,
QString*)': test.cpp:8:21: error: invalid operands of types 'QString*'
and 'QString*' to binary 'operator+'
QString result = p + n;
How can I make this work? Also using += instead of + doesn't works here.
~ Jan
Indeed you are adding their address as p and n are pointer. try adding their value as:
QString result = *p + *n;
i know this question has been answered in this forum before, but I need more specific help.
Here's the code:
sessionwindow.cpp
#include "sessionwindow.h"
#include "ui_sessionwindow.h"
#include "session.h"
#include "utils.h"
#include <QStringList>
SessionWindow::SessionWindow(QWidget *parent) : QDialog(parent), ui(new Ui::SessionWindow)
{
ui->setupUi(this);
this->setFixedSize(this->size());
}
SessionWindow::~SessionWindow()
{
delete ui;
}
void SessionWindow::on_cancelBtn_clicked()
{
close();
}
void SessionWindow::on_createBtn_clicked()
{
QString min = (ui->isMin) ? "min" : "nomin";
QString sp = (ui->spHidd) ? "nosp" : "sp";
QString name = ui->sessionName->text();
QString user = ui->skUser->text();
QString pass = ui->skPass->text();
Utils u;
u.createSession(name, user, pass, min, sp);
}
utils.h (Where the function prototype is declared)
#ifndef UTILS_H
#define UTILS_H
#include <QString>
#include <QStringList>
class Utils
{
public:
Utils();
~Utils();
void startSkype(QString, QString, QStringList);
void createSession(QString , QString, QString, QString, QString);
};
#endif // UTILS_H
utils.cpp (Where the function is)
#include "utils.h"
#include "session.h"
#include <QString>
#include <QStringList>
#include <QVector>
#include <QDebug>
QVector<Session> sessions;
Utils::Utils()
{
}
Utils::~Utils()
{
}
void Utils::startSkype(QString user, QString pass, QStringList options)
{
}
void createSession(QString name, QString user, QString pass, QString isMin, QString spHid)
{
sessions.append(Session(name, user, pass, isMin, spHid));
}
The problem is tha I can't compile it, it just throws an error: undefined reference to `Utils::createSession(QString, QString, QString, QString, QString)'
Sorry if I explained bad and thanks for the help!! :D
You forgot to put Utils:: in front of your method definition:
void createSession(QString name, QString user, QString pass, QString isMin, QString spHid)
{
sessions.append(Session(name, user, pass, isMin, spHid));
}
should be
void Utils::createSession(QString name, QString user, QString pass, QString isMin, QString spHid)
{
sessions.append(Session(name, user, pass, isMin, spHid));
}
I hope is not a question too specific... But I will be really thankfull if you could help!
when I build my application I get this error, do you know why?:
ld: duplicate symbol colors::white in mainwindow.o and main.o
collect2: ld returned 1 exit status
Here is the definition of the main:
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
and here is the definition of main window:
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
glwidget = new MyGLBox(ui->centralWidget);
startTimer(11);
//test frame
BasicShapes shapes;
Polygon3 square = shapes.create_square(V3(0.,0.,0.),V3(1.0,1.0,1.),colors::cyan);
Polygon3 cube = shapes.create_cube(V3(-1.,-1.,-1.),V3(1.,1.,1.),colors::purple);
Polygon3 triangle = shapes.create_triangle(V3(0,1.,0),V3(1.,1.,1.),5.,colors::green);
//other stuff...
ui->vLayoutGLview->addWidget(glwidget); //add the glwidget to the ui framework
}
colors is a namespace defined in the file colors.h:
namespace colors{
Color white(1.,1.,1.,0.);
Color black(0.,0.,0.,0.);
Color red(1.,0.,0.,0.);
Color green(0.,1.,0.,0.);
Color blue(0.,0.,1.,0.);
Color yellow(1.,1.,0.,0.);
Color purple(1.,0.,1.,0.);
Color cyan(0.,1.,1.,0.);
}
here is my file list:
colors.h
#include <string>
#include <iostream>
#include <sstream>
mainwindow.h
#include <QMainWindow>
#include "myglbox.h"
#include "sceneobject.h"
mathvector.h
#include <vector>
#include <cmath>
#include <string>
#include <iostream>
#include <sstream>
#include "colors.h"
myglbox.h
#include <QGLWidget>
#include <QtOpenGL>
#include <vector>
#include "mathvector.h"
sceneobject.h
#include "mathvector.h"
I'm pretty sure I didn't included two time the same file(but maybe I missed it), and anyway the header are protected by the header guards.
do you have any idea why I get a duplicate id error? without the namespace it compiled fine, but a namespace with the standard color will be really useful...
Objects defined in a header file will be duplicated in every compilation unit that includes that header, leading to duplicate definition errors. These should either be extern (and defined elsewhere), or (my preference) made into free functions:
Colors.h
namespace color
{
const Color& white();
const Color& black();
// etc...
}
Colors.cpp
#include "colors.h"
namespace color
{
const Color& white()
{
static Color w(1.,1.,1.,0.);
return w;
}
const Color& black()
{
static Color b(0., 0., 0., 0.);
return b;
}
}
Then you can use them easily as:
Color white_copy = colors::white();
const Color& black = colors::black();