Static function declared but not defined in C ++

I get an error from the following code using C ++.

main.cpp

#include "file.h" int main() { int k = GetInteger(); return 0; } 

file.h

 static int GetInteger(); 

file.cpp

 #include "file.h" static int GetInteger() { return 1; } 

The error I get is:

 Error C2129: static function 'int GetInteger(void)' declared but not defined. 

I read the famous article, "Organizing a C and C ++ Code File , " but I don’t understand what is wrong with this code.

+46
c ++ static-methods
May 30 '12 at 8:37
source share
6 answers

In C ++, static in the global / namespace area means that a function / variable is used only in the translation unit where it is defined, and not in other translation units.

Here you are trying to use a static function from a different translation unit ( Main.cpp ) than the one in which it is defined ( File.cpp ).

Remove static and it should work fine.

+87
May 30 '12 at 8:41
source share

Edit

 static int GetInteger(); 

to

 int GetInteger(); 

static in this case gives the internal linkeage method, which means that you can only use it in translation units, where you define it.

You define it in File.cpp and try to use it in main.cpp , but main has no definition for it since you declared it static .

+19
May 30 '12 at 8:41 a.m.
source share

Since in this case static means that the function name has an internal relationship; that GetInteger in one translation unit is not related to GetInteger in any other translation unit. The static overloaded: in some cases, it affects life expectancy, and in others, on binding. This is especially embarrassing here, because "static" is also the name of a lifespan. Functions and data declared in a namespace scope always have a static lifetime; when static appears in their declaration, it causes internal binding, not external.

+6
May 30 '12 at 8:45
source share

If everything is in the same translation system, it should work. You probably did not compile File.cpp into the same block as Main.cpp.

 g++ -Wall File.cpp Main.cpp 

If each file is compiled separately, the function must be made extern , which will be used from another translation unit.

 extern int GetInteger(); 

which coincides with

 int GetInteger(); 
+2
May 30 '12 at 8:45
source share

functions declared as static arelocal to the contained file. Therefore, you need to define the function in the same file as those who call it. If you want to make it callable from another file, you should NOT declare it as static.

+1
May 30 '12 at 8:42
source share

From my point of view, static functions are the name distorted by the name of the file in which they are defined, so when you include file.h in main.cpp, GetInteger () gets mangled with main.cpp, although you defined GetInteger () in the .cpp file, but since it is static, it is also distorted, and the linker cannot find the definition of GetInteger (), since no function by this name exists.

I believe that the lesson learned does not declare static functions in the header file because they are not intended for part of the interface.

+1
May 30 '12 at 8:47
source share



All Articles