无法将参数 1 从 'Class *' 转换为'double []'

Cannot convert parameter 1 from 'Class *' to 'double []'

本文关键字:转换 double Class 参数      更新时间:2023-10-16

好吧,这是我在这里得到的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:"fill":无法将参数 1 从"City *"转换为"int" []'

不知何故,我看到,它说类"城市*"和 int[] 数组有问题,但无法弄清楚。我用双倍但相同的概率更改"int"。这是问题,否则它似乎是文件中一个简单的数组填充函数。知道它有什么问题吗?

那么,怎么会

空隙填充(City&arr,int size) 的身体想要?

void fill(int arr[], int size)意味着您需要将int数组传递给fill。但是,您正在向它传递一个实例 City ,特别是在您的主中hm

我假设您正在尝试从cities.txt中读取City描述列表(因此具有适当的流提取/插入运算符)。更改 fill 的签名以接受指向City对象的指针,以便您可以填充 City 对象的数组,而不是 int s。

void fill(City *arr, int size); 

确保传递给 main 中fill的参数是一个正确分配的数组,其中包含大小至少为 sz City 个对象,其中 sz 是作为第二个参数传入的内容。请记住调用delete []以释放您创建的此数组。

一种更惯用的方法是使用vector<City>这样您就不必担心内存管理问题。修改后的fill签名将是:

void fill(std::vector<City>& c); // note we no longer need the second argument

不过,您需要#include <vector>才能使用vector<City>

最后,要利用 RVO,只需按值返回vector<City>,而不是将其作为参数传入。因此,您可以执行以下操作:

std::vector<City> fill(); // cleaner, faster

void fill(int arr[], int size)需要一个int数组。您正在尝试将其传递给City数组。

您可以编写一个新函数,void fill(City& arr, int size)也可以考虑编写模板化函数,template<class T> void fill(T* arr, size)等。

还可以考虑使用 std::vectorstd::array 而不是裸数组。边界检查和内存管理将容易得多。

以下是几个fill示例:

// keep reading til we run out of cities in the file
void fill_vector(vector<City>& cities)
{
    ifstream ifs("cities.txt."); 
    City city;
    while (!ifs.fail() && !ifs.bad())
    {
        ifs >> city;
        cities.push_back(city);
    }
}
// only read 'n' cities
void fill_array(array<City, 5>& cities, size_t count)
{
    ifstream ifs("cities.txt."); 
    for (size_t i = 0; i < count; i++)
        ifs >> cities[i];
}

你用错了fill .您想要的函数是 <algorithm>void fill (ForwardIterator first, ForwardIterator last, const T& value); 中的模板化版本(请参阅此处)。

因此,为了使用默认City对象初始化每个元素,您的调用将变为:

std::fill(hm, &(hm[n]), City());

或(更具可读性并显示意图):

std::fill(&(hm[0]), &(hm[n]), City());

编辑:我现在看到我误解了你的问题,你想用存储的对象填充你的hm数组。

正如其他答案中所建议的那样,将fill函数的签名更改为fill (City * const arr, size_t const n)应该可以解决这个问题。