Static function?

Hi all!

I want to define a method which can be used by all the classes in my solution, like a global variable but a "global method". I have been reading and the closest concept I found was Static function. Is it the proper solution to what I am looking for? Is there anything better?

And just for the sake of curiosity: why everyone calls it static function?? Shouldn't it be static method since we are in a object-oriented language?

Thanks in advanced!
A static function is what you are looking for.
Often 'function' and 'method' are used to mean the same thing.
In my opinion "member function" is the same as "method". (Not everyone in the engineering community agrees on the use of "method".)
Ok thank you but I am getting some problems compiling. Specifically I am getting a: error LNK2019... it says something about external symbol which cannot be resolved.

I defined the static method in class Frame:

1
2
3
static void hello(){
	cout<<"Hello";
}


and then in a different class I write:

Frame::hello();

When i compile I get that ugly error :(.

Can anyone help please??

Thanks

Static function: Is a function, but defined as a method, but this is what you wanna use..
since we are in a object-oriented language

What? C++ is not an OO language. It supports OOP.
closed account (D80DSL3A)
Did you #include "Frame.h" in the other class? You may need to.
Isn't Frame.h another math function? If it is you know all you have to do is

#include <cmath>
Last edited on
@rever
Can you replicate your situation? where/how did you declare/define/call that function?
@Richard101
WTF?
Also, this may be not-so-obvious, where you defined the 'hello()' function you may need to write it this way:

 
std::cout << "Hello";


That is unless you did a 'using namespace std;' Without seeing the specific error I'm just guessing and thinking of what I can.
Hi!

Sorry for answering so late!! Thank you all for the replies but I still have the problem.

I think I have defined correctly the static function:
1
2
3
static void hello(){
	cout<<"Hello";
}

because as long as I don't use it anywhere the compiler says nothing.

When I type it somewhere in my code (yes, Frame.h is included and it is not a math function):
 
Frame::hello();


I get that error in the compiler:
 
error LNK2019:  unresolved external symbol "public: static void __cdecl Frame::hello(void)" (?hello@Frame@@SAXXZ)...


I finally gave up and did it without a static function but now I am curious about where was the error. BTW, thanks again.
Can you explain more your situation? source-header files / inlined functions etc
@Bazzy What do you mean?
Last edited on
Frame.h is a file, not a math function
I get a link error when I declare a function, but don't define it... Then try to use it.
1
2
3
4
class Frame {
 public:
  static void hello(); //declaration, but not a definition (see the ;)
};

Try:
1
2
3
4
5
//tell the compiler that hello is a member of Frame
// (note that you only need the keyword "static" when declaring the function)
void Frame::hello() {
  cout << "Hello";
}


FYI: you can create a global function that doesn't belong to a class.
It would be smart to put it in a namespace though...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

namespace my_space {
  using std::cout;
  
  //hello is now a global function and a member of my_space, full name is my_space::hello
  void hello() {
    cout << "Hello";
  }
}


using namespace my_space;

int main() {
  hello();
  return 0;
}
Yes, I found where the mistake was, thanks Mathhead200! I was using the word static in both the definition and the declaration!!

Now it works properly :) (I keep the namespace option in my mind, but first I need to completely understand the concept of namespace)
No problem. I'm glad it worked.

A namespace is just a way to avoid name conflicts (i.e. be able to have more then one variable named the same thing)
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
namespace A {
	int n; //because n is defined in namespace A its full name is A::n
	double x; //same here, A::x
}

namespace B  {
	int n; //you can't have two variables with the same identifier; however
		// here because n is being declared inside namespace B its full name is B::n 
		// which is not the same as the A::n defined above
}

#include <iostream>

//our main function
int main() {
	A::n = 10; //in order to refer to are first n (defined in namespace A)
		// we need to prefix the variable's name with the namespace's name
		// (i.e. used its full name)
	A::x = 3.14; //same here
	B::n = -1 //we are using a different variable here (the second n, the one from namespace B)

	//note that we have to use cout's full name because we omitted the line "using namespace std;" 
	std::cout << A::n << '\n';
	std::cout << A::x << '\n';
	std::cout << B::n << '\n';

	int n = 7; //here the full name of this variable is n (no prefix)
		// because it's declared in the global namespace

	std::cout << n << '\n'; //uses the n from the global namespace
	// std::cout << x << '\n'; //Error! there is no x in the global namespace
	// std::cout << B::x << 'n'; //Error! x is not defined in namespace B
}

Example's output:
10
3.14
-1
7


So by defining your function hello in your own namespace, you ensure that any other hello functions defined will not break your program.
Last edited on
Topic archived. No new replies allowed.