Reputation: 65
I'm learning c++ and I have the following code which gives an error in line 39 (fill_file() call). I've searched on the web for a solution but can't find why I get this error (expected primary-expression before '&' token).
#include <iostream>
#include <string>
#include <vector>
#include "../std_lib_facilities.h"
using namespace std;
struct Point {
double x;
double y;
};
void fill_file(vector<Point>& original_points) {
string outputfile="mydata.txt";
ofstream ost{outputfile};
if(!ost) error("Can't open outputfile ", outputfile);
for(int i=0;i<original_points.size();i++) {
ost << original_points[i].x << " " << original_points[i].y << endl;
}
}
int main() {
cout << "Please enter 3 points with a value: " << endl;
vector<Point> original_points;
Point p;
double x;
double y;
for(int i=0;i<3;i++) {
cin>>p.x;
cin>>p.y;
original_points.push_back(p);
}
cout << endl;
cout << endl << "Points: " << endl;
for(int i=0;i<original_points.size();i++) {
cout << original_points[i].x << " " << original_points[i].y << endl;
/* ost << original_points[i].x << " " << original_points[i].y << endl; */
}
cout << endl << endl;
fill_file(vector<Point>& original_points);
return 0;
}
What am I doing wrong? Thx for the help!!
Upvotes: 2
Views: 15299
Reputation: 11787
You made an error calling the function fill_file()
. Currently you call it like this:
fill_file(vector<Point>& original_points);
This above, I presume is a copy paste error. What I thing you want to do is:
fill_file(original_points);
because original_points
is the actual variable, not vector<Point>& original_points
. As your error states:
expected primary-expression before '&' token
As seen above, you are putting a random l-value in the function call, and this is not allowed.
Upvotes: 4
Reputation: 1123
You made a mistake when you called your fill_file function:
fill_file(vector<Point>& original_points);
must be called like this:
fill_file(original_points);
Upvotes: 6