Problem with private data.
Aug 18, 2011 at 12:59pm UTC
Hello,
When I tried to write simple OOP program with Qt, I got an error and actually I don't know how to resolve it.
I wrote a simple class called Dispatcher. Here is code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include "dispatcher.h"
#include <QFile>
#include <QTextStream>
#include <QDebug>
Dispatcher::Dispatcher()
{
this ->file_name = "sample.cpp" ;
this ->data = QVector<QString>();
this ->prepared = "" ;
}
void Dispatcher::retrieveFile(QString &path)
{
QFile file(path);
QTextStream in(&file);
QString line;
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug() << "An error has occured by opening file: " << path << endl;
}
while (!in.atEnd()) {
line = in.readLine();
this ->data.append(line);
}
}
QString Dispatcher::getDataAt(int i)
{
if (this ->data.size() <= i){
return this ->data.at(i);
} else return QString("" );
}
#ifndef DISPATCHER_H
#define DISPATCHER_H
#include <QString>
#include <QVector>
class Dispatcher
{
public:
Dispatcher();
void retrieveFile(QString& path);
QString getDataAt(int);
private:
QString file_name;
QVector <QString> data;
QString prepared;
};
#endif // DISPATCHER_H
----------
part of main.cpp:
Dispatcher* d = new Dispatcher();
d->retrieveFile(pattern_path);
for(int i = 0; i < d->data.size(); i++){
qDebug() << d->getDataAt(i);
}
And an error msg:
/home/piotrek/dev/qt/scs_finder-build-desktop/../scs_finder/dispatcher.h:20: error: ‘QVector<QString> Dispatcher::data’ is private
I'll be very glad for all hints what's wrong.
greetings,
piotrekd
Aug 18, 2011 at 1:10pm UTC
You will need to give Dispatcher a public member function to retrieve the size of data as you can not get to it here: for (int i = 0; i < d->data.size(); i++)
Aug 18, 2011 at 1:16pm UTC
That was it, thanks :)
Topic archived. No new replies allowed.