Undefined reference to a static function

I have a strange problem when I create a static function in class A and I want to call it from a function of class B. I get

undefined reference to `A :: funcA (int) '

Here is my source code: a.cpp

#include "ah" void funcA(int i) { std::cout << i << std::endl; } 

hijras

 #ifndef A_H #define A_H #include <iostream> class A { public: A(); static void funcA( int i ); }; #endif // A_H 

b.cpp

 #include "bh" void B::funcB(){ A::funcA(5); } 

and bh

 #ifndef B_H #define B_H #include "ah" class B { public: B(); void funcB(); }; #endif // B_H 

I am compiling with Code :: Blocks.

+11
c ++
source share
2 answers
 #include "ah" void funcA(int i) { std::cout << i << std::endl; } 

it should be

 #include "ah" void A::funcA(int i) { std::cout << i << std::endl; } 

Since funcA is a static function of your class A This rule applies to both static and non-stationary methods.

+23
source share

You forgot the definition prefix with the class name:

 #include "ah" void A::funcA(int i) { ^^^ //Add the class name before the function name std::cout << i << std::endl; } 

As you did something, you defined unbound funcA() , resulting in two functions (namely A::funcA() and funcA() , the first of which was undefined).

+6
source share

All Articles