0

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

1
  • 2
    "Passing a variable to a header file" doesn't make much sense. A header file is literally just a bunch of source code that gets copy-pasted into your .cpp when it's #included. Please give an example of what you're having trouble with. Commented Apr 30, 2011 at 15:03

3 Answers 3

3

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. :)

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.