Hello.

I have two classes

//Ah #ifndef A_H #define A_H #include <QtGui> #include <QDebug> #include "ui_A.h" #include "Bh" #include <QLine> #include <QCoreApplication> class A : public QDialog, public Ui::DialogClass { Q_OBJECT public: A(QWidget* parent = 0); ~A(); bool getValue() { return _value; } public slots: private slots: private: B* b; bool _value; protected: void paintEvent(QPaintEvent *); }; #endif // A_H //Bh #ifndef B_H #define B_H #include <QtGui> #include "ui_B.h" //class Dialog; class B : public QWidget, public Ui::MyWidgetClass//,public QObject { Q_OBJECT public: B(QWidget *parent = 0); ~B(); //B(QObject *parent = 0); protected: signals: public slots: private: A* my1; }; #endif // B_H //B.cpp #include <QDebug> #include "Ah" B::B(QWidget *parent) : QWidget(parent) { setupUi(this); } B::~B() { } void B::on_button1_clicked() { //if(my1->getVaue() == true)//Как тут использовать getvalue()?? //my1->getValue() = false; // else // my1->getValue() = true; update(); // qDebug()<<"text"; } //main.cpp #include "Ah" #include <QtGui> #include <QApplication> #include "Bh" int main(int argc, char *argv[]) { QApplication app(argc, argv); A a; //B my1; a.show(); return app.exec(); } 

Question. How to use the _value field from class A in class B?

  • one
    Hmm, it seems to be the opposite: if instance B wants to have access to instance A , then accordingly in class B should be a pointer to A - VladD
  • one
    Well, this is just an easy fix. In bh remove the #include "Ah" and just add class A; . And in b.cpp just the opposite add #include "Ah" . - VladD
  • 2
    No one is confused by the entry "class A:: public QDialog, public Ui :: DialogClass" and "class B:: public QWidget", and that at the end of the ads there is no semicolon? This will not compile. When you create an instance of class B, you can pass a pointer to an instance of class A to it in the constructor, and from class B methods you can access the attributes of an instance of class A by calling QObject :: parent () and then casting the type to class A. - Alexei Naumov
  • 2
    In void B :: on_button1_clicked (), the pointer my1 is not initialized. - Alexei Naumov
  • 2
    @marioxxx: isn't it, is it initialization ? Your pointer my1 equals something incomprehensible, and you already dereference it. You first initialize the variable my1 , and my1->_isBeingMouse1=false; - this is a reference to a variable. - VladD

3 answers 3

It seems like it should fly.

// Ah

 #ifndef A_H #define A_H #include <QObject> class A : public QObject { Q_OBJECT private: bool _value; public: A(QObject* parent = 0) : QObject(parent), _value(true) {} bool getValue() { return _value; } void setValue(bool value) { _value = value; } }; #endif // A_H 

// Bh

 #ifndef B_H #define B_H #include <QObject> #include "Ah" class B : public QObject { Q_OBJECT private: A* my1; public: B(QObject *parent = 0); void on_button1_clicked(); void printValue(); public slots: }; #endif // B_H 

//B.cpp

 #include <QtDebug> #include "bh" B::B(QObject *parent) : QObject(parent), my1(new A()) { } void B::on_button1_clicked() { if(my1->getValue()) my1->setValue(false); } void B::printValue() { qDebug() << "Value is " << my1->getValue(); } 

//main.cpp

 #include <QCoreApplication> #include "bh" int main(int argc, char** argv) { QCoreApplication app(argc, argv); B b; b.on_button1_clicked(); b.printValue(); return app.exec(); } 
  • Well, of course. A semicolon after the definition of the class in each header should be put. - Illia Sernikov
  • By the way, do you have a Q_OBJECT macro in classes? - Illia Sernikov
  • corrected code. And laid out an example that compiles normally. The on_button1_clicked () method can be pushed into the slot later. printValue () - just to see that everything has changed. - Illia Sernikov
  • like this, I have a full look at the code .. - marioxxx
  • And he should not know anything about V. So all references to class B, in the Ah header, need to be removed. include "Ah" to write in Bh, not in B.cpp. In main, why do you use a class A object if you need a class B object? Class B encapsulates class A. Therefore, the outside world does not know anything about class A and works with it only through a class B object. - Illia Sernikov

Judging by the code, you can easily declare class A friend ( friend ) of class B or, better yet, declare as friends certain methods of class B that use _value . Then, from your friends , you will be able to access the private members of class A

At the same time, the implementation will still be hidden from all other classes and entities.

To do this, in the declaration of class A simply add the necessary friend directives. Here, for example, how to declare a method friendly.

 class A { friend void B::changeValue(); private: int _value; }; class B { void changeValue() { a->_value = 2; } private: A *a; }; 
  • swears on line private A a; "bh: 38: error: ISO C ++ forbids declaration of 'A' with no type" and "bh: 38: error: expected ';' before ' ' token "in the private A * a; In the string friend B :: on_button1_clicked (); "Ah: 17: error: ISO C ++ forbids declaration of 'on_button1_clicked' with no type" "Ah: 17: error: prototype for 'int B :: on_button1_clicked ()' Bh: 31 : error: candidate is: void B :: on_button1_clicked () " - marioxxx
  • one
    @marioxxx, it seems you need to specify the type of the return value of the method when declaring friend . friend void B :: on_button1_clicked (); - khaos
  • Now swears on the line private A * a; "bh: 38: error: ISO C ++ forbids declaration of 'A' with no type" and "bh: 38: error: expected ';' before '*' token ". If anything, #include "Ah" is registered in Bh. Ah written in #include "Bh". - marioxxx
  • one
    @marioxxx, maybe even write the code for you? Most likely, the name of the constructor of class A does not coincide with the name of the class itself. - khaos
  • Thanks for trying to answer. - marioxxx

Thanks to everyone for the answers and advice, all +1!) To resolve this issue, parts of the answers from the participants were used, for which they are very thankful.) Initialization has been added.

  //Ah #include <QtGui> #include <QDebug> #include "ui_A.h" #include "Bh" class A { private slots: friend void B::on_button1_clicked(); private: int _value; }; //Bh #ifndef B_H #define B_H #include <QtGui> #include "ui_B.h" class A; class B : public QWidget, public Ui::MyWidgetClass { Q_OBJECT public: B(QWidget *parent = 0); ~B(); protected: signals: public slots: void on_button1_clicked(); private: A* my1; }; #endif // B_H //B.cpp #include <QDebug> #include "Ah" #include "Bh" B::B(QWidget *parent) : QWidget(parent),my1((A*)parent) setupUi(this); } B::~B() { } void B::on_button1_clicked() { if(my1->_value == true) my1->_value = false; else my1->_value = true; update(); } //main.cpp #include "Ah" #include <QtGui> #include <QApplication> int main(int argc, char *argv[]) { QApplication app(argc, argv); A a; a.show(); return app.exec(); } 
  • You can also add Ah friend class B; and in the B.cpp file my1 -> _ value = false; so that false is at the beginning. - marioxxx