L
L
l2p2014-09-09 15:06:56
Qt
l2p, 2014-09-09 15:06:56

How to call a main class method from another class?

There is a method Class::method();
How can I call it from another Class2 class? Declared the header class.h in the second class, but still writes that Class wasn't declared.
Tried passing a pointer to the Class2 constructor like Class *pp. When declaring a class, I entered this. Still an error occurs. What am I doing wrong?
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

4 answer(s)
E
EXL, 2014-09-09
@EXL

Again, without having studied the C ++ base and the Qt documentation, you are trying to solve the task at hand. The C++ programming language is not the language where this approach can be used. You either know the basics well, or you don't know anything at all.
You need to take advantage of data exchange between classes. This can be done in various ways: getters-setters, double inheritance, passing a pointer, a signal-slot system, a common class for data exchange, static methods, etc.
There are many examples on the Internet:
easy-qt.blogspot.ru/2012 /10/1.html
www.cyberforum.ru/qt/thread548059.html
fkn.ktu10.com/?q=node/3021
www.prog.org.ru/topic_19557_0.html
And you have a rather strange approach. The form should query the boolean class, not the other way around. That is, your non-GUI-class should not contain anything and pull Qt's. Suddenly it will have to be moved to a separate library that has nothing to do with GUI and Qt?
I highly recommend looking at the sample applications that come with Qt. There you will find all the answers to your questions and methods for building the right application architecture.

I
Ivan Ivanov, 2014-09-09
@Csklassami

@l2p In that case, @kachsheev was right. Here is an example:

class MyClass
{
public:
  void MethodFromClass1() {  //метод для теста
    cout << 666;
  }
};

class MyClass2 : public MyClass
{
public:
  void MethodFromClass2() {
    MethodFromClass1();  //вызов метода из производного класса
  }
};

int main()
{
  MyClass2 obj;
    obj.MethodFromClass2();
  return 0;
}

If you want to make a method call and not use main(), then nothing will come of it ... the compiler only executes instructions from the main function.

A
Anton Kashcheev, 2014-09-09
@kachsheev

class.h:

class Class
{
public:
    void mathod();
}

include "Class.h"
Class2.h:
class Class2
{
public:
    void method2()
    {
        Class class1;
        class1.method();
    };
}

main.cpp:
#include "Class2.h"

int main()
{
    Class2 class2;
    class2.method2();
    return 0;
}

Of course, somewhat exaggerated, because. no constructors or destructors. But in theory it should work.
Or what did you mean?

A
AxisPod, 2014-09-10
@AxisPod

Isn't it easier to read a few small articles on the basics in the time that you have to wait for an adequate answer to an inadequate question?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question