compiler error declaring function

It must be time to enroll in remedial C++...I can't figure out what I'm doing wrong with this super-simple example.

In main.cpp():
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <fstream>

void	fileOpen(ifstream &in);
void	parseLine(string);

using namespace std;

int main()
{


In fileopen.cpp:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <fstream>

using namespace std;

void fileOpen (ifstream &p_in)
{
	ifstream p_in("data.csv");
	return;
}


I'm getting an error:
error: declaration of 'std::ifstream p_in' shadows a parameter


Would someone please point out the dum-dum I'm committing?

Thanks.
Line 8 is declaring a local variable of the same name as a parameter passed to the function.
OK, I did catch that, but when I change it to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*
 * return a pointer to an ifstream.
 */
#include <iostream>
#include <fstream>

using namespace std;

void fileOpen (ifstream &is)
{
	ifstream in;
	in.open("airline.csv");
	is = in;
	return;
}


I get a ton of errors occurring in library files, and one for my file:

synthesized method 'std::basic_ifstream<char, std::char_traits<char> >& std::basic_ifstream<char, std::char_traits<char> >::operator=(const std::basic_ifstream<char, std::char_traits<char> >&)' first required here


I know I'm doing something really dumb; I just can't see it.
don't create a second file in this function. Just open the file that was passed to it:

1
2
3
4
5
6
void fileOpen (ifstream &is)
{
   // you already have file 'is' given to this function, so use it:
   is.open("airline.csv");
   return;
}
OK, thanks, that got rid of that problem. All my errors are now in main():

error: variable or field 'fileOpen' declared void
../Lab1/main.cpp:4: error: 'ifstream' was not declared in this scope
../Lab1/main.cpp:4: error: 'in' was not declared in this scope
../Lab1/main.cpp:5: error: variable or field 'parseLine' declared void
../Lab1/main.cpp:5: error: 'string' was not declared in this scope
../Lab1/main.cpp: In function 'int main()':
../Lab1/main.cpp:14: error: 'fileOpen' was not declared in this scope
../Lab1/main.cpp:19: error: 'parseLine' was not declared in this scope
../Lab1/main.cpp:23: error: 'rc' was not declared in this scope
../Lab1/main.cpp:25: error: return-statement with no value, in function returning 'int'
make: *** [main.o] Error 1


Here's main() in its entirety:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <fstream>

void	fileOpen(ifstream &in);
void	parseLine(string &s);

using namespace std;

int main()
{
	ifstream	in;
	string		line;
	int		rc;

	fileOpen(in);
	if (in)
	{
		while (getline (in, line))
		{
				parseLine(line);
		}
	}
	else
		rc = 1;

    return;
}
void parseLine(string &line)
{
	cout << line << endl;
	return;
}
Last edited on
when using the return keyword, specify what you are returning.
Return 0 for your main function, and if a function has a void return type, don't use return keyword.

To use string you must #include<string>
Last edited on
Thanks for catching that. I'm still getting the errors, though.
ifstream, string are in the std namespace. They need to be prefixed with std::. So std::ifstream.

You can get around that by dumping all of the std namespace into the global namespace (ie, with using namespace std;) but that only applies to code further down in the cpp file.

Basically... your using line is in the wrong spot:

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <fstream>

using namespace std;  // <- put it here

void	fileOpen(ifstream &in);
void	parseLine(string &s);

// using namespace std;  <- not here 
Ahhhhhhhh.

Thanks so much, Disch. I guess that problem wasn't quite as dumb as I thought, but...I still should have known better. I appreciate the explanation.
Topic archived. No new replies allowed.