C++ Cannot call member function inside the class
c++, static-methods
Solution
You can only call other static member functions from inside a static member function. This is because a non-static member function is associated with a particular instance of the object. Here you don't have an instance.
So one quick fix is to make `printHello` static:
static void printHello(){
std::cout << "Hello" << std::endl;
}
If you need `printHello` to work on a particular instance of `A` (because perhaps in your real code it uses a non-static data member), then you need to create that instance. You could do that like so:
static void astaticFunction(){
A a; // Create an A object
a.printHello();
}
Problem
I'm wondering why it's not possible to call a member function from a static function ``` #include <iostream> class A{ public: A(){} ~A(){} static void astaticFunction(){ printHello(); } private: void printHello(){ std::cout << "Hello" << std::endl; } }; int main(int argc, char **argv){ A::astaticFunction(); return 0; } ``` the compiler is returning this ``` main.cpp: In static member function ‘static void A::astaticFunction()’: main.cpp:8:16: error: cannot call member function ‘void A::printHello()’ without object ``` And how can I use it ? Thanks