无法将参数 1 从 'Class *' 转换为'double []'
Cannot convert parameter 1 from 'Class *' to 'double []'
好吧,这是我在这里得到的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::vector
或 std::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)
应该可以解决这个问题。
- C++在数学计算中将double转换为int
- 有没有办法在C++的赋值中将"char**"转换为"double"?
- 错误:无法将"<lambda(double)>"转换为"double(*)(double)"
- 如何正确将"strings"转换为"double"?
- Zedboard zynq-7000 Opencl 浮点数从类型 'double*' 强制转换为类型 'double' 无效
- "std::function<double(double)>"到"double (*)(double)"之间的转换
- C++ 模板函数无法将"std:string"转换为"double"作为回报
- 缩小从double到float的转换
- 在不复制数据的情况下,将double数组转换为只有double成员的structs数组
- 将 mwArray 转换为 std::vector<double>
- 使用词法强制转换在'string to double'中设置双精度变量的精度
- 如何将 Java double[][] 转换为 C++ <vector<vector>> JNI?
- 从 std::vector<double> 到 Python 列表的转换失败(提升 python)
- 转换 cv::mat 转换为 std::矢量<double>并发症
- 将双数组转换为标准::复杂<double>
- 将 std::vector<boost::optional<double>> 转换为 std::vector<double>
- 将 double* 转换为 std::vector<double> (C++)
- 无法将复数<double>转换为双精度
- 将 int 转换为双精度以执行 std::complex <double>的"*"操作
- "double(*)(string, double, double)"尝试传递函数C++时"double"转换错误