C++ QT4 compile error

//#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.
Dmon443Asked:
Who is Participating?
 
ZoppoCommented:
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
 
Dmon443Author Commented:
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
 
ZoppoCommented:
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
 
Dmon443Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.