c ++变量的多个定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11072244/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:48:40  来源:igfitidea点击:

c++ multiple definitions of a variable

c++compiler-errors

提问by Bg1987

I have 4 files (2 headers, and 2 code files). FileA.cpp, FileA.h, FileB.cpp, FileB.h

我有 4 个文件(2 个标题和 2 个代码文件)。FileA.cpp、FileA.h、FileB.cpp、FileB.h

FileA.cpp:

文件A.cpp:

#include "FileA.h"

int main()
{
    hello();
    return 0;
}

void hello()
{
    //code here
}

FileA.h:

文件A.h:

#ifndef FILEA_H_
#define FILEA_H_
#include "FileB.h"
void hello();

#endif /* FILEA_H_ */

FileB.cpp:

文件B.cpp:

#include "FileB.h"

void world()
{
    //more code;
}

FileB.h:

文件B.h:

#ifndef FILEB_H_
#define FILEB_H_

int wat;
void world();


#endif /* FILEB_H_ */

when I try to compile(with eclipse), I get " multiple definition of `wat' " And I don't know why, it seems as it should work just fine.

当我尝试编译(使用 eclipse)时,我得到“`wat' 的多重定义”而且我不知道为什么,看起来它应该可以正常工作。

回答by Richard J. Ross III

I'm not going to include all of the details, but you define a global variable, wattwice in your compilation uint.

我不打算包括所有细节,但您wat在编译 uint 中定义了一个全局变量两次。

To fix, use the following:

要修复,请使用以下命令:

FileB.h

文件B.h

extern int wat;

FileB.cpp

文件B.cpp

int wat = 0;

This (extern) tells the compile that the variable watexists somewhere, and that it needs to find it on it's own (in this case, it's in FileB.cpp)

这个 ( extern) 告诉编译器变量wat存在于某处,并且它需要自己找到它(在这种情况下,它在FileB.cpp

回答by Cornstalks

Don't declare the variable in the header. #includeliterally copies and pastes the contents of the file into the other file (that is, any file that does #include "FileB.h"will literally copy the contents of FileB.h into it, which means int watgets defined in every file that does #include "FileB.h").

不要在头文件中声明变量。#include从字面上复制并粘贴文件的内容到另一个文件中(也就是说,任何文件#include "FileB.h"都会将 FileB.h 的内容复制到其中,这意味着int wat在每个文件中都定义了#include "FileB.h")。

If you want to access watfrom FileA.cpp, and it's declared in FileB.cpp, you can mark it as externin FileA.cpp.

如果wat要从 FileA.cpp访问,并且在 FileB.cpp 中声明,则可以将其标记为extern在 FileA.cpp 中。

回答by Mahesh

You get multiple definition because watis declared at file scope and get's visible twice in the 2 source files.

您会得到多个定义,因为wat在文件范围内声明并且在 2 个源文件中可见两次。

Instead, make the variable declartion externand define it in exactly onesource file.

相反,进行变量声明extern并将其定义在一个源文件中。

extern int wat;  // In FileB.h

int wat;   // In FileB.cpp

回答by Bg1987

I found the answer now (I guess looking at the files one after another helped) The problem is that the compiler creates a FileB.o which has a definition of wat, and then it tries to compile FilB.o with FileA.cpp, while FileA.h has an include of FileB.h it will now also have a definition of wat.

我现在找到了答案(我想一个接一个地查看文件有帮助)问题是编译器创建了一个 FileB.o,它定义了 wat,然后它尝试使用 FileA.cpp 编译 FilB.o,而FileA.h 包含 FileB.h,它现在也包含 wat 的定义。

回答by Johannes Schaub - litb

That wat variable declaration is a definition that should belong in a cpp file. It is as if you would put your function bodies into the header.

那个 wat 变量声明是一个应该属于 cpp 文件的定义。就好像您将函数体放入标头一样。

You need to put extern before the wat declaration and copy your current declaration without the extern into one of the cpp files.

您需要将 extern 放在 wat 声明之前,并将没有 extern 的当前声明复制到 cpp 文件之一中。