Reputation: 21
I am trying to read the txt file into the vector in function readIn. I cannot for the life of me get it to work. Either I get the following message "error: expected primary-expression before '&' token readIn(ifstream& infile, vec);" or the function isn't called at all.
int main() {
const int MAXSIZE = 100;
vector<int> vec (MAXSIZE);
ifstream infile;
infile.open("in9.3.txt");
readIn(ifstream& infile, vec);
return(0);
}
void readIn(ifstream& infile, vector<int> &vec) {
int a, count;
count = 0;
while (!infile.eof()) {
infile >> a;
vec.at(count) = a;
count++;
}
infile.close();
vec.resize(count);
}
Upvotes: 1
Views: 56
Reputation: 63471
You must not specify the type of the parameter when passing to a function. What you have written is incorrect:
readIn(ifstream& infile, vec); // error
Note that you are trying to pass the variable infile
which is defined in main
. The compiler is complaining about the fact that you prefixed this with ifstream&
. The correct call is:
readIn(infile, vec);
Also beware that since the function is defined after main
, there must be a function declaration somewhere before main
. It is not clear whether you did this or not, since you have not shown a complete program. In any case, you can either move the whole definition before main
, or just add this line:
void readIn(ifstream&, vector<int>&);
Upvotes: 3