c++菜鸟。。想编一个折半查找的小程序,遇到个问题。。求助
代码是这样的#include<iostream>
class Data{
public:
Data(int data[]);
int Binary_Search(int a,int low,int high);
private:
int data;
};
Data::Data(int data[]){
for(int i = 0;i<5;i++){
this->data=data;
}
}
int Data::Binary_Search(int a,int low,int high){
int mid =(low+high)/2+1;
while(low<high){
if(data<a){
low = mid;
mid =(low+high)/2+1;
return Binary_Search(a,low,high);
}
if(data>a){
high=mid;
mid =(low+high)/2+1;
return Binary_Search(a,low,high);
}
if(data=a){
return mid;
}
}
}
int main(){
int a;
int temp;
std::cout<<"请输入5个数";
for(int i=0;i<5;i++){
std::cin>>a;
}
for(int j=0;j<5;j++){
for (int k=5;k>j;k--){
if(a>a){
temp=a;
a=a;
a=temp;
}
}
}
for(int i=0;i<5;i++){
std::cout<<a;
}
Data d(int a[]);
int x;
std::cout<<"请输入要查找的数";
std::cin>>x;
int located=d.Binary_Search(x,0,4);//这里出错 报错是 request for member 'Binary_Search' in 'd', which is of non-class type 'Data(int*)'
std::cout<<"您要查找的数位于数组中的第"<<located<<"个位置";
} d的初始化吧直接Data d(a)呢
页:
[1]