How to call function from the original class
-
Hello, I have a Qt Designer Form class. Let's call it A. It has A.cpp and A.h , and of course a class named A.
For some reasons I created another class inside A.cpp, call it class B. I made a global object b from class B.Functions inside class A can easily call class B functions by b.somefunction();
but when a class B function wants to call a class A function it can't.
an error message said: call to non-static member function without an object argument.I guess there must be an object of class A or everything will make no sense, but what is it?
-
@MasterBlade said in How to call function from the original class:
I guess there must be an object of class A or everything will make no sense, but what is it?
Yes, you need an instance (object) of a class to call its non static methods:
A a; a.doSomething();
a is here the instance (object).
-
@jsulm said in How to call function from the original class:
@MasterBlade said in How to call function from the original class:
I guess there must be an object of class A or everything will make no sense, but what is it?
Yes, you need an instance (object) of a class to call its non static methods:
A a; a.doSomething();
a is here the instance (object).
Actually I tried that and it worked. But isn't there a default instance when running the program? Or how can the window appear?
-
@MasterBlade
Yes, there may already be a instance.
But you need a way to get access to it, like a pointer or something.
You cannot just call a non-static member function of nothing. -
@MasterBlade said in How to call function from the original class:
But isn't there a default instance when running the program?
No, why should there be a default instance? It's your responsibility as developer to create one when needed.
If you're talking about windows then take a look at main.cpp - you will see how main window is created there. Maybe you should explain what exactly you're trying to do?