ifstream and function type

I have two questions about a function that I am writing for a program.

First, when I compile it gives the error:

error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::ifstream' (or there is no acceptable conversion).

I do not understand what the problem is. Isn't using ">>" how you retrieve data from ifstream?

My second question is: what type should I use for the function name? In the past I have written functions that only use one type of object, so I would use the same type for the function. What do I use if I have multiple types of objects in the function?

My function:

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
type fillVectors(const string & inFileName, vector<string> & nameVec, vector<double> & gradeVec)
{

ifstream inStream;
inStream.open(inFileName.data());
if (inStream.is_open())
{
	int count = 0;
	for (;;)
	{
		inStream >> nameVec >> gradeVec;
                count++;
		
                if (inStream.eof()) break;
			
	        string name;
		double grade;
			
		nameVec.push_back(name);
		gradeVec.push_back(grade);
	}
	
        inStream.close();
}
else
	cout << "Unable to open file.";
}




Isn't using ">>" how you retrieve data from ifstream?

It is, though you can't use it to retrieve data into a vector directly, which is why the compiler is complaining. Instead, you should be doing inStream >> on name and grade.

what type should I use for the function name? ... What do I use if I have multiple types of objects in the function?

That type is the return type. Here, through the reference parameters nameVec and gradeVec, your function is producing side effects. You can have a function that produces side effects as well as returns a value. In this case, your function doesn't return anything so the return type should be void.
Thanks for the help
Topic archived. No new replies allowed.