[英]c++ error: expected primary-expression before '&' token
I'm learning c++ and I have the following code which gives an error in line 39 (fill_file() call). 我正在学习c ++,我有以下代码,它在第39行(fill_file()调用)中给出了错误。 I've searched on the web for a solution but can't find why I get this error (expected primary-expression before '&' token). 我在网上搜索了一个解决方案,但无法找到我收到此错误的原因(预期在'&'标记之前的primary-expression)。
#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!! 谢谢!!
You made a mistake when you called your fill_file function: 调用fill_file函数时出错了:
fill_file(vector<Point>& original_points);
must be called like this: 必须像这样调用:
fill_file(original_points);
You made an error calling the function fill_file()
. 调用函数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
. 因为original_points
是实际变量,而不是vector<Point>& original_points
。 As your error states: 正如您的错误所述:
expected primary-expression before '&' token 在'&'标记之前预期的primary-expression
As seen above, you are putting a random l-value in the function call, and this is not allowed. 如上所示,您在函数调用中放置了一个随机的l值,这是不允许的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.