好吧,这是我在这里得到但无法编译的 C++ 代码
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int n = 900;
class City{
string name;
double area;
int count, roads;
public:
City() {}
City(string a, double b, int c, int d ) { a=name; b=area; c=count; d=roads;}
string getname() {return name;}
double getarea() {return area;}
int getcount() {return count;}
int getroads() {return roads;}
friend ostream& operator << (ostream& , City& );
friend istream& operator >> (istream& , City& );
};
ostream& operator << (ostream& out, City& a) {
out<<"name "<<a.name<<", area "<<a.area<<", count "<<a.count<< ", roads "<<a.roads<<endl;
return out;
}
istream& operator >> (istream& in, City& a) {
in>>a.name>>a.area>>a.count>>a.roads;
return in;
}
void fill(int arr[], int size){
ifstream ifs("cities.txt.");
for (int i=0;i<size;i++)
ifs>>arr[i];
}
void func(City* arr){
ofstream ofs("density.out");
for(int i=0;i<n;i++){
if(arr[i].getcount()/arr[i].getarea()>1000)
ofs<<arr[i];
}
}
int main(){
City* hm;
hm = new City[n];
fill(hm, n);
func(hm);
system ("pause");
return 0;
}
这是我在编译时遇到的错误:
错误 C2664:“填充”:无法将参数 1 从“城市 *”转换为“int []”
不知何故,我看到了,它说 Class 'City *" 和 int[] 数组有问题,但无法弄清楚。我用 double 但相同的概率更改了 'int'。这是问题,否则看起来很简单文件中的数组填充函数。知道它有什么问题吗?
那么,怎么会
void fill(City& arr, int size) 的身体想要什么?