C++ 错误 C2084 '函数已经有一个主体'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21080453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error C2084 'Function already has a body'
提问by FuNK01
I am a novice at C++ coding and this is my first post on Stack Overflow.
我是 C++ 编码的新手,这是我在 Stack Overflow 上的第一篇文章。
I am coding a text based game in Visual C++ and I have been getting the C2084 error in this file:
我正在用 Visual C++ 编写一个基于文本的游戏,我在这个文件中收到了 C2084 错误:
//game_system_functions.cpp
#include "basicdefines.h"
namespace
{
using namespace std;
void clear_console()
{
if (system("CLS")) system("clear");
}
}
I find this odd, because it is only happening in ThisFile. All the other .cpp files have no problem with functions and they are all in a similar format.
我觉得这很奇怪,因为它只发生在这个文件中。所有其他 .cpp 文件在函数方面都没有问题,它们都采用类似的格式。
I have checked and double checked all my code, and there is no other function called clear_console. I have tried renaming the function to a bunch of random letters, and I still get the same error. Other functions in the same .cpp file get a similar error. This issue has been bothering me for the past week, and I can't solve it.
我已经检查并仔细检查了我所有的代码,并且没有其他名为 clear_console 的函数。我尝试将函数重命名为一堆随机字母,但仍然出现相同的错误。同一个 .cpp 文件中的其他函数也会出现类似的错误。这个问题在过去一周一直困扰着我,我无法解决。
I have read other posts about error C2084
and they aren't having this problem.
I would appreciate some help regarding this error.
我已经阅读了其他帖子error C2084
,他们没有遇到这个问题。我将不胜感激有关此错误的一些帮助。
Thank you.
谢谢你。
P.S. I apologize about any formatting problems, as I said earlier in the post, this is my first time posting on stack overflow and sorry if the title is a little undescriptive.
PS 我对任何格式问题深表歉意,正如我之前在帖子中所说的那样,这是我第一次在堆栈溢出上发帖,如果标题有点不描述,我很抱歉。
回答by Sebastian Hoffmann
OP:
操作:
I am intending it to hold all the string and console manipulation functions for the entire game, so almost every file in the program has it included
我打算用它来保存整个游戏的所有字符串和控制台操作功能,所以几乎程序中的每个文件都包含它
And this is the problem. Its a .cpp
file not a .h
. It actually contains definitions of symbols.
If you now include this file, its verly likely that some other files, you also include, include this file aswell.
这就是问题所在。它是一个.cpp
文件而不是.h
. 它实际上包含符号的定义。如果您现在包含此文件,则您也包含的其他一些文件很可能也包含此文件。
What happens is that in the preprocessed unit code like this occur:
发生的事情是在这样的预处理单元代码中发生:
void clear_console()
{
if (system("CLS")) system("clear");
}
void clear_console()
{
if (system("CLS")) system("clear");
}
The error message makes sense now, doesnt it? To solve this you will have to use a header guard
错误信息现在说得通了,不是吗?要解决这个问题,您将不得不使用头卫
Or better, fix your file structure: A .cpp
should never be included. Instead create a header file with e.g this declaration void clear_console();
. In a .cpp
you then implement the function like you did already, but you only include the header (.h
) file.
或者更好的是,修复您的文件结构:.cpp
永远不应该包含A。而是使用例如此声明创建一个头文件void clear_console();
。在 a 中,.cpp
您可以像之前一样实现该功能,但您只包含头 ( .h
) 文件。
Also notice that this isnt possible with anonymous namespace, but they dont make any sense at all here. So just use a regular / named namespace or get rid of it.
还要注意,这对于匿名命名空间是不可能的,但它们在这里根本没有任何意义。所以只需使用常规/命名命名空间或摆脱它。
回答by Roddy
You do have a separate .h
file that corresponds to this .cpp
file, don't you?
您确实有一个与.h
此.cpp
文件相对应的单独文件,不是吗?
If not, and you try and #include "game_system_functions.cpp"
in other files, you will have problems because your .cpp
file has no header guards.
如果没有,并且您尝试#include "game_system_functions.cpp"
在其他文件中使用,则会遇到问题,因为您的.cpp
文件没有标题保护。
If you're including a file into others, you should have header guards something like:-
如果您将文件包含在其他文件中,则应该具有类似以下内容的标题保护:-
#ifndef GAMESYSTEMFUNCTIONS_H
#define GAMESYSTEMFUNCTIONS_H
...
... function declarations, etc..
...
#endif