PDA

View Full Version : Issue with getting a QString output through a function



mvanstad
12th February 2016, 06:35
Hi

I have a problem where I can't seem to get a output to display in a console when doing it through a function.

It works when doing it through Main(), but just blank when doing it through the function.

Below is some of my code:




//Main()

#include "ConferencePaper.h"
#include "JournalArticle.h"
#include "Reference.h"
#include <QDebug>
#include <QTextStream>

QTextStream cout(stdout);

int main()
{
//QApplication app(argc, argv);
QStringList list1;
list1 << "This is a test";

Reference a("Marius",list1,1,"c"); //Instance of the Reference class created with parameter values
cout << "Title: " << a.getTitle(); //This works fine
a.toString();

return 0;

}


//Reference Function


#include <QString>
#include <QStringList>
#include <QTextStream>

#include "Reference.h"

Reference::Reference(QString ti, QStringList as, int ye, QString id): title(ti), authors(as), year(ye), refID(id){}

QString Reference::toString()
{
return QString("Title: %1\n") .arg(getTitle()); //Does not display anything

}


Thanks

yeye_olive
12th February 2016, 09:11
Reference::toString() does not print anything; it just builds and returns a QString. The call

a.toString();
in main() therefore builds a QString and throws it away immediately. What you probably intended was to print the QString returned by the call, as in:

cout << a.toString();

If I were you I would choose another name for the QTextStream; readers of your code will assume cout refers to std::cout.