?
Solved

C++ QT4 compile error

Posted on 2012-08-22
4
Medium Priority
?
382 Views
Last Modified: 2012-08-29
//#include <QtCore/QCoreApplication>
#include <QTextStream>
#include "student.h"

static QTextStream cout(stdout, QIODevice::WriteOnly);

void graduate(Student* student) {
    cout << "\nThe following "
         << student->getClassName()
         << " has graduated\n "
         << student->toString() << "\n";
}

int main()
{
    //QCoreApplication a(argc, argv);
    Undergrad us("Frodo", 5562, "Ring Theory", 4);
    GradStudent gs("Bilbo",3029, "History", 6, GradStudent::fellowship);
    cout << "Here is the data for the two students:\n";
    cout << gs.toString() << endl;
    cout << us.toString() << endl;
    cout << "\nHere is what happens when they graduate:\n";
    graduate(&us);
    graduate(&gs);
    return 0;
}

Open in new window

#include <QTextStream>
#include "student.h"

Student::Student(QString nm, long id, QString major, int year)
    :m_Name(nm), m_Major(major), m_StudentId(id),m_Year(year) {}

QString Student::getClassName() const   {
    return "Student";
}

QString Student::toString() const   {
    QString retval;
    QTextStream os(&retval);
    os << "[" << getClassName() << "]"
            << " name: " << m_Name
            << " Id: " << m_StudentId
            << " Year: " << yearStr()
            << " Major: " << m_Major;
    return retval;
}

Undergrad::Undergrad(QString name, long id, QString major, int year)
    : Student(name, id, major, year)
    { }

QString Undergrad::getClassName() const  {
    return "Undergrad";
}

GradStudent::GradStudent(QString nm, long id, QString major, int yr, Support support)
    :Student(nm, id, major, yr), m_Support(support) { }

QString GradStudent::toString() const  {
    QString result;
    QTextStream os(&result);
    os << Student::toString()
            << "\n  [Support: "
            << supportStr(m_Support)
            << "] \n";
    return result;
}

Open in new window

#ifndef STUDENT_H
#define STUDENT_H

#include <QString>

class Student   {
public:
    Student(QString nm, long id, QString m_Major, int year = 1);
    ~Student()  {}
    QString getClassName() const;
    QString toString() const;
    QString yearStr() const;
private:
    QString m_Name;
    QString m_Major;
    long m_StudentId;
protected:
    int m_Year;
};

class Undergrad: public Student {
public:
    Undergrad(QString name, long id, QString major, int year);
    QString getClassName() const;

};

class GradStudent : public Student  {
public:
    enum Support { ta, ra, fellowship, other };
    GradStudent(QString nm, long id, QString major, int yr, Support support);
    QString getClassName() const;
    QString toString() const;
protected:
    static QString supportStr(Support sup);
private:
    Support m_Support;
};

#endif // STUDENT_H     //#ifndef STUDENT_H

Open in new window

I am getting a compile error in QT4  "collect2: ld returned 1 exit status". Please help, can't figure out where the error is.
0
Comment
Question by:Dmon443
  • 2
  • 2
4 Comments
 

Author Comment

by:Dmon443
ID: 38322393
I am getting a compile error in QT4  "collect2: ld returned 1 exit status". Please help, can't figure out where the error is.
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 38323881
Hi Dmon443,

first this is not a compile error, it's a linker error. The info you gave isn't enough to help you much - most probably the cause for the error is shown before this error message is written, i.e. if the linker cannot find a referenced function it would look somehow like this:

Undefined symbols:
  "foo", referenced from:
      _alert in bar.o
ld: symbol(s) not found


This means a function foo is declared somewhere and used in bar.c but the linker wasn't able to find it anywhere.

Could you post the part of the output preceeding the error message?

ZOPPO
0
 

Author Comment

by:Dmon443
ID: 38324460
Hi Zoppo,

Actually there is no output besides that line, collect2 error is all I get in build issues, should I be looking somewhere else to find more info?
0
 
LVL 31

Accepted Solution

by:
Zoppo earned 1000 total points
ID: 38324523
Hm - usually the linker should give some info about what's going wrong. Maybe you can set some flag which makes the linker generate more verbose output.

Anyway, I took a look at the code you posted (I hope it's the complete code) and found there are some problems:

These functions are declared but not implemented:

Student::yearStr()
GradStudent::getClassName()
GradStudent::yearStr()

Further I guess you forgot to declare these functions as virtual:

Student::yearStr()
Student::getClassName()
Student::toString()

As it is now the functions called in void graduate(Student* student) always are from class Student, never from one of the derived classes.

ZOPPO
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question