Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I like to pass char* parameter (file ) to a function and use the file variable to create object type fstream.



int C_Utility::Sub_file_open (char *file, char *option) {
	fstream file; //create object of fstream class
        file.open(file, ios::in);
...


The above code gives me
file.open(file, ios::in);
std::fstream file’ shadows a parameter
  fstream file;  ... 


What I have tried:

fstream wants char , so string does not work either.
Posted
Updated 18-Feb-20 10:55am
v2

Do not give the same name to the parameter of the method and the inner variable; you named both file, use distinct names instead.
 
Share this answer
 
v2
Comments
Vaclav_ 18-Feb-20 16:38pm    
So how do I specify that I want to work on parameter (file) passed ?
Stefan_Lang 19-Feb-20 2:30am    
You can't! You must use a different name for your local variable. I thought the solution was already pretty clear on that.
CPallini 18-Feb-20 16:48pm    
5.
Quote:
So how do I specify that I want to work on parameter (file) passed ?
C++
int C_Utility::Sub_file_open (char *filename, char *option) 
{
  fstream file; //create object of fstream class
  file.open(filename, ios::in);
  //..

or, if you prefer
C++
int C_Utility::Sub_file_open (char *filename, char *option) 
{
  ifstream file(filename); //create object of fstream class
  //..
 
Share this answer
 
v2
Comments
phil.o 18-Feb-20 16:49pm    
5'd :)
C++
file.open(file, ios::in);

You have named your fstream file, and the first parameter, which should be a char* (see basic_fstream Class | Microsoft Docs[^]) you have also named file. So the compiler thinks you are trying to pass a fstream object to the open method of a fstream, which makes no sense.

Don't use names like file for anything as it can easily conflict with actual types. Use sensible meaningful names, it will save you headaches in future.
 
Share this answer
 
Comments
Vaclav_ 18-Feb-20 20:17pm    
Apparently fstream OPENS the file hence I was actually doing it twice and that
helped" to create the conflict. Using "shadowing" instead of "duplicate " helps with my vocabulary , however I would prefer "duplicate"....
Stefan_Lang 19-Feb-20 2:48am    
What do you even mean by 'duplicate' and 'shadowing'? I have a strong feeling that you are trying to use concepts from another language in C++ which doesn't have that concept!? (Also, the last line of code in your OP uses a ' like a comment sign - but in C++ it does not have that meaning!)

In case the three solutions already offered haven't been sufficiently clear:
In C/C++, when you declare a variable with a specific name, all future (i. e. later in the code) references of that name will refer to that specific variable! If you already had a variable using that name, e. g. a function parameter, that object will be unaccessible throughout the rest of your code!

It is a general rule in C/C++, that you should _never_ reuse a name! Even though under certain circumstances the compiler will allow the redefinition of a symbol, doing that never serves a sensible purpose. (and if you think it does, then either you are simply wrong, or you may be thinking of a concept that does not exist in C++)

tl;dr: Do not reuse variable names in C++! Ever.*

* P.S: this applies to a single function - you can of course use the same name in a different function. (Technically, scope, not function, but that's already a bit dangerous)
Richard MacCutchan 19-Feb-20 3:35am    
You forgot to mention scope rules.
Stefan_Lang 19-Feb-20 5:06am    
I skipped them deliberately: Even though you can reuse names temporarily within a limited scope, doing so is just asking for trouble. If there is an actually resonable use case for that, I haven't heard of it.
Richard MacCutchan 19-Feb-20 5:14am    
That's not what I meant. Your comment implies that if you use a name for a variable anywhere in your program you should never use the same name again. But it is OK to use the same name in different scopes. Although there are plenty of people who don't fully understand the concept.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900