Qt general issue accessing a member of another class



  • Hi,
    I'm trying to access a member of a different class i.e. (i have took out all other code due to it being unrelated to my issue)

    header1.h

    @class MainWindow : public QMainWindow
    {
    public:
    MainWindow();

    public slots:
    int open();
    };
    @
    header2.h

    @class SaveWindow : public QTextEdit
    {
    public:
    SaveWindow();

    public slots:
    int save();
    };
    @
    mainWindow.cpp

    @#include "header1.h"
    #include "header2.h"

    int MainWindow::open()
    {
    BLAAA...
    return save();
    ^^^^^^^^^^^^^^^wont let me access save, due to being MainWindow::open and save is not a member of that class! Tried creating an object and trying to access through that but no joy!
    }

    int SaveWindow::save()
    {
    BLAAA...
    }
    @
    There is probably a simple solution.

    doing....
    @
    SaveWindow sw;
    return sw.save();
    @
    also gives me errors.
    Thanks for any help :)

    Edit: added some @ tags to your post to format your code; Andre



  • Please put your code in @ @ (without ) block.

    This should work.
    @
    SaveWindow sw;
    return sw.save();
    @

    But this code doesn't work:
    @
    int MainWindow::open()
    {
    BLAAA…
    return save();
    }
    @
    because MainWindow hasn't a save method.



  • Hi,
    I'm confused, trying to access a member of a different class i.e.

    header1.h

    @class MainWindow : public QMainWindow
    {
    public:
    MainWindow();

    public slots:
    int Open();
    };@

    header2.h

    @class SaveWindow : public QTextEdit
    {
    public:
    SaveWindow();

    public slots:
    int Save();
    };@

    mainWindow.cpp

    @#include "header1.h"
    #include "header2.h"

    int MainWindow::Open()
    {
    BLAAA...
    return Save();
    ////////////////////////////////////////////////////<<<<<<<<<<wont let me access Save, due to being MainWindow::Open and Save is not a member of that class! Tried creating an object and trying to access through that but no joy!
    }

    int SaveWindow::Save()
    {
    BLAAA...
    }@

    Sorry I'm new to Qt and posting



  • Read about some course or book about C++ because you issue is with language, not Qt. If you will had a basic skills with C++ then would be a sense to help you with your code. I think that now you are typing code in blind without understanding.



  • What should work is when you create a variable of the SaveWindow class and call that function.
    @
    int MainWindow::Open(void)
    {
    BLAA...
    SaveWindow saveThing;
    return saveThing.save();
    @
    if that works for you. The saveWindow will be destroyed when the open function quits, so don't worry about that.



  • Posting some code that will likely work is good, but in this case I think I agree more with Hostel since you really need to get your basic "Object Oriented" concepts clear. Otherwise its going to be too frustrated to do anything :)

    So I hope that stuyfozz didn't totally got scare off and is busy reading some basic Object Oriented and C++ tutorials :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.