C++ pass variable from .cpp to header file - c++

this relates to a question i asked previously:
C++ array in header file
in the main.cpp file there is a variable called fin1
ifstream fin1("ACW2_data.txt");
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
Thanks in advance

This variable can be declared in the header file as an extern.
extern ifstream fin1;
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. :)

I think you need to back up and explain what you are trying to do. Header files are, in general, for defining common definitions and declarations.
What do you mean by "use the value in the header file"? In general, the header file is not where code is run. So what needs to use this variable there?
Generally speaking, variables that need to be used in more than one file should be declared in the header to begin with. In C++, this is normally in the form of class members.
Even more common is to pass variables as arguments when another function or method needs to use the same value.
I can't tell from the information you've provided, but it sounds like you're on the wrong track to me.

Declare this variable as extern .
extern ifstream fin1;
Than every time you change it it value gonna update and be ready in your header file
And you can include tour header every where and use that variable

Related

Proper way of passing parameters between UWP pages?

I currently need to pass about 10-15 boolean variables and 1 string variable between 3 and 5 pages in a C++ UWP program. I've tried using global variables in a header file but due to the structure of UWP, it ends up being duplicated in each page. I stumbled across this post on SO: Pass some parameters between pages in UWP
It seems like a viable option but I want to know how to do exactly what is listed in that post but in C++ and with boolean variables. I don't have any idea of how to translate C# code into C++ code but it looks awfully similar. Another big question I have from that post is where do I declare the public class? In the header file? In the .cpp file?
Use extern keyword when declaring variables in the header like: extern int global_var(<--In header). Then, when you are using this variable for the first time, declare it only once in your cpp file like: int global_var(<--First time use in cpp). Then you can reuse this variable by including the header and simply using the variable in your operations global_var=x(<--Any other file where the above header is included) or whatever. It will reflect the change across the program wherever the header is included.

How should I define this variable?

I have several header files .h and their corresponding .cpp files. Here suppose I have part1.h and part2.h for declaration. The corresponding cpp are part1.cpp and part2.cpp for definition the functions.
I also have a file with main. In this main function, I have a variable float * change. As its name, I will change the value of change and then call function F defined in part1.cpp and G in part2.cpp. The problem is I cannot pass change as a parameter.
So at first I plan to define it as a global variable. But then I found this variable always changes. But a global variable only can be defined once. So is there any method to solve this problem?
Thank you in advance.
You can define the variable as normal in one of your .cpp files
float* change;
Then in one of your header files, you can declare it:
extern float* change;
Now #include the header file wherever the global variable is used.
I would recommend to do get/set functions to get variable from whenever you want and you'd keep OOP encapsulation. http://www.cplusplus.com/forum/beginner/107842/
Declare in one of your headers
extern float * change; // does not define the variable, but just that it exists somewhere
You can also declare this directly in part1.cpp and part2.cpp instead of a common header. However if you'd later change something, float to double for example, you should not forget any of those declarations.
Remember Stroustrups famous quote: "// global variable – avoid those where you can"

C++ functions without header file

Do stand alone C++ functions need a header file and a code file?
I am writing some C++ functions that use some of my other C++ classes but don't belong in a class themselves. They are intended to be compiled in a dll and so I was wondering if it was necessary to declare them in a separate header file or if I could/should just put the declarations in the .cc file.
Maybe this would just be bad practice?
The header file is useful because it is able to let other source files know about the functions that are declared in a different translation unit.
This is necessary by the compiler to be able to check that what you are invoking is correct for the type checker. But the necessity comes from the declaration itself not from the existence of the header file.
For a DLL, if I remember correctly, you are not forced to do it just because you are going to declare the signature of the function anyway whenever you are using them, eg.
extern C __declspec(dllimport) void foo();
Of course this means that you will need to forward declare them anyway so I don't see any problem in having an header files for your DLL, it will just keep all signatures together.
It is not strictly necessary, but it is strongly recommended.
Place the function declarations in a header file and the function definitions in a source (.cc) file. The motivation is to allow the users (here, fellow programmers) to view only the interface but not the implementation (since it can change). Moreover, it allows other source files to include your header file and use the functions provided by you.
The only exception are static functions, they should not be declared in a header file because they are not supposed to be viewed or used outside your source file..
If you are going to use a function outside of the source file in which it's defined, you absolutely need a declaration. It is best to put the declaration in a header file so that it's consistent, otherwise you just end up repeating yourself and introducing a potential source of bugs.

How to "hide" a variable inside the header file?

The header file contains two variables.
Because of the structure of my program, I have two "ld: duplicate symbol" errors.
These two variables have only local significance.
Is there any way of making these variables "private", so they wouldn't be seen outside of the header file even if the header file is included to another source file?
EDIT: please tell me, would it be nice if I will put the variables to the cpp file? These variables are very big arrays, defined while initializing, and take a lot of lines of code...
extern char Lookup[][3] = { "aa", "ab", "ac", "ad", "ae", "af", ... and so on (really long)}
The solution is to not define variables in your header file.
If you absolutely must share variables between internal source files (and I recommend that you don't), then you should do the following:
Create an "internal.h".
Declare your variable extern in that header file.
Include "internal.h" in both your internal source files.
Define the variable in one or other internal source files.
The variable is now hidden from the outside world. (It's probably still visible in your object files, but you can use platform-specific trickery to strip it.)
Do not define variable in headers.
Use extern to declare a variable in a header without defining it.
I'm always weary about variables which are "on the loose". I mean: they do affect something don't they? They "belong" to a class?
Shouldn't you just declare them under a class, and then declare them as static variables? (And given the syntax, probably constants too)? In that case you can simply use everything normally done with static variables (initializer lists, static initialize function etc). Seems to me much more clearer, as now your variable is tied with something.

passing values between 2 different c++ files in same project

noob question right here. How do you pass values between 2 different cpp files in the same project? Do you make objects? if yes, how does the other cpp file see it?
some enlightment pls..
EDIT: some clarifications. I'm trying to interface direct input with a program (of which I have the plugins sdk). I'm trying to interface a joystick with it. It seems that there is no main function when I look through the code, but I might be wrong (like, I might not look in the right files). I know programming, and pointers and stuff, classes. Is there anything I should learn or get into in order to achieve what I want?
In all but few cases it's a bad idea to share data among compilation units. A compilation unit, just to get you up to speed with the C++ terminology, usually effectively refers to an implementation file (with extension .cpp, or .cc etc.). The way we have the various compilation units "communicate" with each other is with header files and functions, rather than raw data.
Suppose we have an implementation file main.cc and a second implementation file human.cc. We want main.cc to communicate with human.cc. Here we go:
// main.cc
#include "human.hh"
int main()
{
make_the_human_dance(60);
return 0;
}
// human.hh
void make_the_human_dance(int duration);
// human.cc
#include "human.hh"
void make_the_human_dance(int duration)
{
// define how a human dances properly
// ...
}
Under the same principle you can use classes for communication. Declare the class in the header file and define the class' methods in the implementation file. Sometimes you must provide the implementation of functions in the header files, but that is already going offtopic.
You could declare a global variable in a header file like so:
extern int GlobalVar;
And in exactly one compilation-unit (cpp-file) you have to initialize it:
int GlobalVar = 5;
Any other compilation unit that includes the header now shares the same instance of the global variable (I hope that syntax is correct, i rarely use it).
One should mention, that your question indicates a general lack of understanding of how programs in C++ should be organized. In short, you usually have a file called main.cpp that contains the entry-point of your program. The rest, in C++, is done in classes most of the time. A class is usually split into two parts, the declaration of the class in a header file, and the implementation of the class in a cpp file. To use a class, you include the corresponding header file. This is the short version, and there is much more to tell, but this should give you a good starting point.
Normally you define a function in one cpp file, then declare that function as extern in a header, and include the header in whatever other cpp file needs to use the function. You can then write code that calls the function normally. At link time you need to supply the object files that resulted from both cpp files, and the linker ...links them together, so the function call in one file passes the value correctly as you call the function that was defined in the other cpp file.
Referencing code in a different file typically makes use of #include