![](/img/trans.png)
[英]C++ error: expected primary-expression before ‘.’ token
[英]c++ error: expected primary-expression before '&' token
我正在学习c ++,我有以下代码,它在第39行(fill_file()调用)中给出了错误。 我在网上搜索了一个解决方案,但无法找到我收到此错误的原因(预期在'&'标记之前的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;
}
我究竟做错了什么? 谢谢!!
调用fill_file函数时出错了:
fill_file(vector<Point>& original_points);
必须像这样调用:
fill_file(original_points);
调用函数fill_file()
出错了。 目前你这样称呼它:
fill_file(vector<Point>& original_points);
以上,我认为是复制粘贴错误。 我想做的事情是:
fill_file(original_points);
因为original_points
是实际变量,而不是vector<Point>& original_points
。 正如您的错误所述:
在'&'标记之前预期的primary-expression
如上所示,您在函数调用中放置了一个随机的l值,这是不允许的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.