在头文件 C++ 中声明变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38942013/
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
Declaring variables in header files C++
提问by Kareem Aboughazala
I'm trying to create a simple program that takes input from the user in C++ using good programming practices. It consists of Input.hpp, Input.cpp and main.cpp. I keep getting a multiple definition error even though I am using ifndef to prevent that.
我正在尝试创建一个简单的程序,该程序使用良好的编程实践在 C++ 中接受用户的输入。它由 Input.hpp、Input.cpp 和 main.cpp 组成。即使我使用 ifndef 来防止这种情况发生,我也会不断收到多重定义错误。
Input.hpp
输入.hpp
#ifndef Input_HPP
#define Input_HPP
#include <string>
#include <vector>
using namespace std;
vector<string> Get_Input();
vector<string> input_array;
string starting_position;
int input_number;
#endif
Input.cpp
输入文件
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include "Input.hpp"
using namespace std;
vector<string> Get_Input()
{
cin>>starting_position;
cin>>input_number;
for (int i = 0; i < input_number; i++)
{
cin>>input_array[i];
}
cout<<"Done";
return input_array;
}
main.cpp
主程序
#include "Input.hpp"
#include <iostream>
using namespace std;
int main()
{
Get_Input();
return 0;
}
When I remove the variable declarations from the header file and put them in the cpp file but keep the function declaration in the header file the program builds without errors. It is my understanding that variables and functions can be declared in header files. Can someone please explain to me what I am missing?
当我从头文件中删除变量声明并将它们放在 cpp 文件中但将函数声明保留在头文件中时,程序构建时不会出错。我的理解是变量和函数可以在头文件中声明。有人可以向我解释我缺少什么吗?
Thank you.
谢谢你。
回答by SHR
The Header file isn't very smart, it just tells the pre-processor to take the whole header and put it instead of the include line.
头文件不是很聪明,它只是告诉预处理器获取整个头文件而不是包含行。
if you do that, you can see the variable is declared twice.
如果你这样做,你可以看到变量被声明了两次。
To solve the issue, you should declare the variables in one of your cpp files and use extern
in the headers.
要解决此问题,您应该在其中一个 cpp 文件中声明变量并extern
在标头中使用。
like in input.cpp:
就像在 input.cpp 中一样:
int input_number;
and in input.hpp:
在 input.hpp 中:
extern int input_number;
回答by Mohamed Moanis
The include guard only prevent copying the included file if it was already copied which is working correctly in your code and the compiler can compile the code successfully. Now what you getting is a linker error, after your compiler has generated the object files for Input.cpp
and main.cpp
it will find two symbols -variables- with the same name and will start to complain which one should I use?
如果包含的文件已经在您的代码中正常工作并且编译器可以成功编译代码,则包含保护只会阻止复制包含的文件。现在你得到的是一个链接器错误,在你的编译器生成了目标文件之后Input.cpp
,main.cpp
它会找到两个同名的符号 - 变量 - 并开始抱怨我应该使用哪个?
So to sum up when you define a variable in a header file add the extern
keyword to keep the linker happy.
总而言之,当您在头文件中定义变量时,请添加extern
关键字以使链接器满意。