【发布时间】:2014-03-28 02:55:07
【问题描述】:
所以,我正在尝试使用函数填充和打印一个小数组,但是我遇到了一些障碍。我的代码是:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
struct planes
{
string name;
int seats;
int range;
int weight;
};
int populate (planes planesArray[5])
{
string name;
int seats, range, weight, i;
ifstream infile;
infile.open("plane_data.txt");
if (!infile)
{
cout << "File cannot be reached";
}
for (int i = 0; i < 4; i++){
infile >> name;
infile >> seats;
infile >> range;
infile >> weight;
}
infile.close();
}
int main() {
planes planesArray[5];
populate(planes planesArray[5]);
};
我在使用的不同代码迭代中遇到大量错误。有了上面粘贴的这个,我得到:
line 44: error: expected primary expression before (planesArray)
说实话,我有点失落。数组中有 5 条数据,我只是不知道如何使用我创建的函数可靠地将文件中的数据获取到数组中。
任何帮助将不胜感激!
【问题讨论】:
-
那行应该只是说
populate(planesArray);。您不应该在每次使用变量时都提及它的类型。 -
@BrianBi 现在我已经为函数头做了这个,它说我需要 , 或 ;在 { 令牌之前。
-
在函数头中,您确实需要类型。将其视为函数参数的声明。
标签: c++ arrays pass-by-value