PDA

View Full Version : cannot call member function 'void window::updateStatus(QString)' without object



Astrognome
18th August 2012, 02:32
I've been googling for a while, and I can find no solution to my problem. I want to access items in the ui from different files, but haven't had any luck. But anyway here's the code:

main.cpp


#include <QtGui/QApplication>
#include "window.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
window w;
w.show();

return a.exec();
}


window.cpp



#include "window.h"
#include "ui_window.h"
#include "worldBackup.h"

#include <QString>

window::window(QWidget *parent) :
QWidget(parent),
ui(new Ui::window)
{
ui->setupUi(this);
}

window::~window()
{
delete ui;
}

void window::updateStatus(QString text)
{
ui->statusBar->setText(text);
}

void window::on_quitButton_clicked()
{
listWorlds();
}



worldBackup.cpp



#include "window.h"
//#include "ui_window.h"

bool listWorlds(){
window::updateStatus("Status: Quitting...");
return true;
}


window.h



#ifndef WINDOW_H
#define WINDOW_H

#include <QWidget>

namespace Ui {
class window;
}

class window : public QWidget
{
Q_OBJECT

public:
explicit window(QWidget *parent = 0);
~window();

void updateStatus(QString text);

private slots:
void on_quitButton_clicked();

private:
Ui::window *ui;
};

#endif // WINDOW_H


worldBackup.h



#ifndef WORLDBACKUP_H
#define WORLDBACKUP_H

bool listWorlds();

#endif // WORLDBACKUP_H


The error is thrown by line 5 of backupWorld.cpp

ChrisW67
18th August 2012, 07:40
The solution to your problem is C++ knowledge and nothing to do with Qt. window::updateStatus() is not a static member function of the window class and therefore cannot be called without a instance of the window class, i.e. an object. This is exactly what the error message is telling you. You need to call this function through a pointer or reference to the w object you create in main().

Astrognome
18th August 2012, 07:49
I feel very silly right now. I switched from python not too long ago.