【发布时间】:2015-01-15 23:32:40
【问题描述】:
我的问题正如我在标题中指定的那样:test_median.cpp:
在函数“int main()”中:test_median.cpp:26:27:错误:无法转换 'char*' 到 'int*' 用于参数 '1' 到 'int median(int*, int)'
array2 = median(array,size);
这是我的代码。这是测试代码;
#include <iostream>
#include <cmath>
#include "IpFunctions.h"
using namespace std;
int main()
{
int size;
cout<< "size gir" <<endl;
cin >> size;
int i;
char array[size];
cout<< "değerleri gir"<<endl;
for (i=0;i<size;i=i+1)
{
cin >> array[i];
}
char array2[size];
array2 = median(array,size);
cout<<"array2 = "<<array2<<endl;
return 0;
}
这是中位功能代码:
#include <iostream>
#include <cmath>
using namespace std;
int median(char array[], int size)
{
int i = 0;
int m,n;
while (i<size)
{
if (array[i]>array[i+1])
{
m = array[i]; n = array[i+1];
array[i] = n; array[i+1] = m;
i=i+1;
}
else
{
i=i+1;
}
}
return *array;
}
最后是“IpFunctions.h”标题:
// this is just a headerfile that you add your Image processing functions' prototypes.
// when testing your functions with a main function
// main needs a prototype of the function
//Image medianfiltering(Image &inimg, const int size );
int median(int array[],int size);
// when you type a function add the header here !!!
// such as
// Image negative(Image &inimg);
所以我只想制作一个函数来获取数组的中位数并返回该数组。
【问题讨论】:
-
函数
median的签名定义与它的声明不同。注意函数参数类型不同。 -
哦,好的,所以我将所有字符都更改为 int 现在我收到此错误 test_median.cpp: In function 'int main()': test_median.cpp:26:8: error: incompatible types in assignment of 'int' 到 'int [(((sizetype)(((ssizetype)size) + -1)) + 1)]' array2 = median(array,size);
-
请尝试理解错误信息。它为您提供有关分配给
median的返回类型的提示。array2是一个字符数组,但median返回int。 -
哦,我看到在将函数用于整数并打印它之后,我只看到一个整数而不是字符串。现在我需要弄清楚如何让字符串重新运行。谢谢你的回答。
标签: c++ pointers char int median