好吧,我真的不知道这个错误是什么意思。 *是给指针的吗?我没有使用指针,所以我不知道发生了什么?这是我的第二门编程课,所以我还是很新。任何帮助将非常感激!!
MAIN.CPP
#include <iostream>
#include "arr.hpp"
int main()
{
int size = getInteger();
int array;
fillArray(array,size);
sortArray(array, size); //THE ERROR COMES UP FOR ALL OF THESE FUNCTIONS.
displayArray(array, size);
binSearch(array, size, value);
return 0;
}
ARR.CPP #include "arr.hpp"
#include <iostream>
int getInteger()
{
int value;
std::cout << "Please enter integer between 10 and 20 for size of array: ";
std::cin >> value;
// check if size is in range
if (value >= 10 && value <= 20)
{
return value;
}
else
{
std::cout << "Error. Please enter a correct value." << std::endl << std::endl;
std::cin.clear(); //clears error from cin.fail()
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //removes old cin input for value
std::cin >> value;
}
return value;
}
void fillArray(int array[], int size)
{
std::srand(static_cast<unsigned>(std::time(0)));
for (int i = 0; i < size; i++)
{
array[i] = (std::rand() % 99)+1;
}
}
void sortArray(int array[], int size)
{
std::sort(array, array+size);
}
void displayArray(int array[], int size)
{
for (int i = 0; i < size; i++)
{
std::cout << array[i] << std::endl;
if((i+1)%5 == 0)
{
std::cout << std::endl;
}
}
}
bool binSearch(int array[], int size, int value)
{
int low = 0;
int high = size - 1;
int mid;
while(low <= high)
{
mid = (low+high) / 2;
if(value == array[mid])
{
return mid;
}
else if (value > array[mid])
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return -1;
}
ARR.HPP
#ifndef arr_hpp
#define arr_hpp
int getInteger();
void fillArray(int array[], int size);
void sortArray(int array[], int size);
void displayArray(int array[], int size);
bool binSearch(int array[], int size, int value);
#endif /* arr_hpp */
最佳答案
array
声明为整数,而不是数组。我假设您要声明一个大小为size
的数组并将其传递给函数。 value
的声明。 要解决此问题,请替换
array
的声明并声明value
:int size = getInteger();
int array[size];
int value;
...
编辑:如评论中所述,以上建议在标准C++中不合法。为了使您的程序可以使用合法的C++进行编译,您可能有两种选择:
size
,例如100,并要求用户输入0到100之间的一个数字。std::vector
,并相应地修改所有函数定义。 关于c++ - C++候选函数不可行: no known conversion from 'int' to 'int *' for 1st argument; take the address of the argument with &,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65758128/