gpt4 book ai didi

c++ - 在 C++ 中声明但未定义的静态函数

转载 作者:IT老高 更新时间:2023-10-28 13:21:20 28 4
gpt4 key购买 nike

我在使用 C++ 的以下代码中遇到错误。

Main.cpp

#include "file.h"

int main()
{
int k = GetInteger();
return 0;
}

文件.h

static int GetInteger();

文件.cpp

#include "file.h"

static int GetInteger()
{
return 1;
}

我得到的错误:

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

我读过著名的文章 "Organizing Code File in C and C++" ,但不明白这段代码有什么问题。

最佳答案

在 C++ 中,全局/命名空间范围内的 static 表示函数/变量仅在定义它的翻译单元中使用,而不在其他翻译单元中使用。

在这里,您尝试使用来自不同翻译单元 (Main.cpp) 的静态函数,而不是定义它的翻译单元 (File.cpp)。

删除 static 应该可以正常工作。

关于c++ - 在 C++ 中声明但未定义的静态函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10812769/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com