0

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;
}
  • 1
    You did not define `lines` in your c++ file. This is needed for static variables. – drescherjm Mar 05 '19 at 16:50
  • Related: https://stackoverflow.com/questions/29223949/why-static-variable-needs-to-be-explicitly-defined – drescherjm Mar 05 '19 at 16:53
  • 2
    You'll need to show how you compile (and, particularly, link) your code. It looks like you forgot to reference the `Qt5Widgets` library. – Toby Speight Mar 05 '19 at 16:53
  • Please see the [How do I ask a good question](https://stackoverflow.com/help/how-to-ask). Moreover, asking for homework assignment is offtopic as can be seen [here](https://stackoverflow.com/help/on-topic) – apalomer Mar 05 '19 at 16:54
  • my apologies for making the question too long i just wanted to give as much information as i could as im really struggling. – Mareliz Van Vuuren Mar 05 '19 at 17:04
  • Usually a question needs to be focused on a single problem and not the entire goal of your program. The reason for this is a question at StackOverflow is supposed to help readers years from now with the same problem. If the scope is too broad it is unlikely that this will help many or that they would even find the question in a search. – drescherjm Mar 05 '19 at 17:18
  • The error message may indicate a problem in your .pro file. – drescherjm Mar 05 '19 at 17:18
  • Thank you so much - drescherjm.. it was a problem in my .pro file and the program runs finally!! thanks so much for everyones feedback i really do appreciate it allot – Mareliz Van Vuuren Mar 06 '19 at 14:40

1 Answers1

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.

N101Seggwaye
  • 75
  • 2
  • 16