C++ QT4 compile error

Posted on 2012-08-22
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";
    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   {
    Student(QString nm, long id, QString m_Major, int year = 1);
    ~Student()  {}
    QString getClassName() const;
    QString toString() const;
    QString yearStr() const;
    QString m_Name;
    QString m_Major;
    long m_StudentId;
    int m_Year;

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


class GradStudent : public Student  {
    enum Support { ta, ra, fellowship, other };
    GradStudent(QString nm, long id, QString major, int yr, Support support);
    QString getClassName() const;
    QString toString() const;
    static QString supportStr(Support sup);
    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.
Question by:Dmon443

    Author Comment

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

    Expert Comment

    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?


    Author Comment

    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?
    LVL 30

    Accepted Solution

    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:


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


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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Suggested Solutions

    Title # Comments Views Activity
    method argument as final 1 54
    scoresIncreasing challenge 10 46
    scores100 challenge 3 66
    countHi challenge 25 57
    INTRODUCTION We all know how to code. But at times you simply want to insert a common code block into your existing code and amend it as per your requirements. This tool not only saves you time but also saves you the pain of typing it all out aga…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    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 fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    794 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now