【问题标题】:cannot convert ‘char*’ to ‘int*’ for argument ‘1’ to ‘int median(int*, int)’ array2 = median(array,size);无法将参数 '1' 的 'char*' 转换为 'int*' 到 'int 中位数(int*, int)' array2 = median(array,size);
【发布时间】: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


【解决方案1】:

在标题中

int median(int array[],int size);

在 .cpp 文件中

int median(char array[], int size)

你的 main 试图调用第一个(它唯一知道的)

【讨论】:

  • 我将所有字符都更改为 int 现在我收到此错误 test_median.cpp: In function 'int main()': test_median.cpp:26:8: error: in compatible types in assignment of 'int' to 'int [(((sizetype)(((ssizetype)size) + -1)) + 1)]' array2 = 中值(array,size);
  • 这是一个新问题。
  • @korel - 阅读错误信息并阅读代码,看中位数的返回类型定义
猜你喜欢
  • 2016-03-15
  • 2015-06-12
  • 2018-11-25
  • 1970-01-01
  • 2016-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-05
相关资源
最近更新 更多