问题描述
在使用C ++的以下代码中出现错误.
I'm getting 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;
}
我得到的错误:
Error C2129: static function 'int GetInteger(void)' declared but not defined.
我已经阅读了著名的文章,但不了解此代码有什么问题.
I've read the famous article "Organizing Code File in C and C++", but don't understand what is wrong with this code.
推荐答案
在C ++中,全局/命名空间范围内的 static
表示该功能/变量仅在定义该功能/变量的翻译单元中使用,不在其他翻译单位中.
In C++, static
at global/namespace scope means the function/variable is only used in the translation unit where it is defined, not in other translation units.
在这里,您尝试使用的静态函数与定义其定义单元( File.cpp
)不同的转换单元( Main.cpp
).
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
).
删除 static
,它应该可以正常工作.
Remove the static
and it should work fine.
这篇关于静态函数已声明但未在C ++中定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!