C ++将变量从.cpp传递到头文件 [英] C++ pass variable from .cpp to header file

查看:183
本文介绍了C ++将变量从.cpp传递到头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这涉及到我之前问过的问题:
头文件中的C ++数组

this relates to a question i asked previously: C++ array in header file

在main.cpp文件中有一个名为fin1的变量

in the main.cpp file there is a variable called fin1

ifstream fin1("ACW2_data.txt");

这可能是一个愚蠢的问题,但是如何使用main.cpp在头文件? (即,是否有一种方法可以在两个文件之间传递变量?)

this might be a stupid question, but how can i use the value of this variable from main.cpp in the header file? (ie. is there a way to pass variables between the two files?)

有关使用头文件的任何其他信息可能有帮助

any other info about using header files may help

提前感谢

推荐答案

此变量可以在头文件中声明为 extern

This variable can be declared in the header file as an extern.

extern ifstream fin1;

现在你可以在 #include 这个头文件包括头文件本身。你不需要传递这样的变量。 :)

Now you can use this variable wherever you #include this header file including the header file itself. You don't need to pass the variable as such. :)

这篇关于C ++将变量从.cpp传递到头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆