预期'('功能样式演员或类型构造 [英] Expected '(' function style cast or type construction

查看:107
本文介绍了预期'('功能样式演员或类型构造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在这里调试代码时,它会显示上面提到的错误。

  int  search( int  A [ ], int  N){
int item,i,beg = 0 ,mid,end;
std :: cout<< 输入元素;
std :: cin>>项目;
for (i = 0 ; i< N; i ++){
结束= N - 1 ;
mid =(beg + end)/ 2 ;
if (A [mid] == item){
std :: cout<< 搜索;
返回项;
} 其他 如果(A [mid]> item){
beg = mid + 1 ;
} else end = mid - 1 ;
}
返回 - 1 ;
}

int main(){
int A [ 90 ],item,i,N,R;
std :: cout<< 输入要搜索的元素;
std :: cin>> item;

std :: cout<< 输入数组的大小 ;
std :: cin>> N;

std :: cout<< 输入数组;
for (i = 0 ; i< N; i ++)
std: :CIN>>一种[I];

R =搜索( int A [], int item);

if (R!= 1
std :: cout<< 成功;
else std :: cout<< 再试一次;
return 0 ;
}



i完全不知道这个..这是二进制搜索的代码,



我尝试了什么:



声明没有参数的数组

解决方案

Quote:

当我在这里调试代码时,它会显示上面提到的错误。



调试时未出现此错误消息。它来自编译器,告诉您存在语法错误。你正在寻找语法错误。

在没有语法错误的情况下进行调试。

调试器用于显示你的代码实际在做什么,以及你必须与你期望它做的比较。

  int  search( int  A [], int  N){
int item,i,beg = 0 ,mid,end;
std :: cout<< 输入元素;
std :: cin>>项目;
for (i = 0 ; i< N; i ++){ // 错误的循环类型
end = N - 1 < /跨度>; // 这不应该在循环中
mid =(beg + end)/ < span class =code-digit> 2
;
if (A [mid] == item){
std :: cout<< 搜索;
返回项;
} 其他 如果(A [mid]> item){
beg = mid + 1 ;
} else end = mid - 1 ;
}
返回 - 1 ;
}



一旦语法错误得到纠正,使用debuger并观察你的代码行为,毫无疑问你会感兴趣。


< blockquote>替换:

R =搜索(int A [],int item);

with:

R =搜索(A,项目);





并替换;

if(R!= 1)

with:

if(R!= - 1)





并更换:

退货;

包含:

退货mid;



另外,您的老师是否希望您使用递归?


请注意,有序数组是二进制搜索的前提条件。

要修改代码,你可以写:

  #include   <   iostream  > ;  
#include < ; 算法 >

bool search( int a [], size_t size , int item)
{
int beg = 0 ;
int end = size- 1 ;

while true
{
< span class =code-keyword> if
((beg + 1 )> = end)
返回(item == a [end] || item == a [beg]);
int mid =(beg + end)/ 2;
if (item == a [mid])
return ;
else if (item< a [mid])
end = mid ;
else
beg = mid;
}
}
枚举 {max_size = 90 };
int main()
{

int 一个[MAX_SIZE];

int 项目;
std :: cout<< 输入要搜索的项目;
std :: cin>>项目;

size_t 大小;
std :: cout<< 输入数组大小;
std :: cin>>尺寸;

if (size> max_size)
{
std :: cerr<< 输入错误\ n;
返回 - 1 ;
}

std :: cout<< 输入数组元素\ n;

for size_t n = 0 ; n< size; ++ n)
std :: cin>>一个];

std :: sort(& a [ 0 ],& a [size]);

bool found = search(a,size,item);
std :: cout<< 项目为<< (找到? found\\\
not found\\\
);
}







然而,在所有程序中,但练习,我会使用火力标准C ++库:

  #include   <   iostream  >  
#include < 算法 >
#include < span class =code-preprocessor> < vector >

int main()
{
int 项目;
std :: cout<< 输入要搜索的项目;
std :: cin>>项目;

size_t 大小;
std :: cout<< 输入数组大小;
std :: cin>>尺寸;

std :: cout<< 输入数组元素\ n;
std :: vector< int> v;
v.resize(size);

for auto & x:v)
std :: cin>> X;

sort(v.begin(),v.end());

auto found = binary_search(v.begin(),v.end(),item);
std :: cout<< 项目为<< (找到? found\\\
not found\\\
);

}


When I debug the code here, it shows the error mentioned above.

int search(int A[],int N) {
    int item, i, beg = 0, mid, end;
    std::cout << "Enter the element";
    std::cin >> item;
    for (i = 0; i < N; i++) {
        end = N - 1;
        mid = (beg + end) / 2;
        if (A[mid] == item) {
            std::cout << "Search found";
            return item;
        } else if (A[mid] > item) {
            beg = mid + 1;
        } else end = mid - 1;
    }
    return -1;
}

int main() {
    int A[90],item,i,N,R;
    std::cout<<"Enter element to be searched";
    std::cin>>item;

    std::cout<<"Enter size of the array";
    std::cin>>N;

    std::cout<<"enter array";
    for(i=0;i<N;i++)
        std::cin>>A[i];

    R=search(int A[],int item);

    if(R!=1)
        std::cout<<"Success";
    else std::cout<<"Try again";
    return 0;
}


i totally have no idea regarding this.. This is a code for binary search,,

What I have tried:

declaring the array without the parameters

解决方案

Quote:

When I debug the code here, it shows the error mentioned above.


This error message don't comes while debugging. It comes from compiler to tell you that there is a syntax error. You are hunting syntax errors.
Debugging comes after that when there is no more syntax error.
The debugger is meant to show you what your code is really doing, and you have to compare with what you expect it to do.

int search(int A[],int N) {
    int item, i, beg = 0, mid, end;
    std::cout << "Enter the element";
    std::cin >> item;
    for (i = 0; i < N; i++) { // wrong kind of loop
        end = N - 1;  // this should not be inside the loop
        mid = (beg + end) / 2;
        if (A[mid] == item) {
            std::cout << "Search found";
            return item;
        } else if (A[mid] > item) {
            beg = mid + 1;
        } else end = mid - 1;
    }
    return -1;
}


Once syntax errors are corrected, use the debuger and watch you code behave, you will be undoubtedly interested.


Replace:
R=search(int A[],int item);
with:
R=search( A , item);
?

And replace;
if(R!=1)
with:
if(R!=-1)
?

And replace:
return item;
with:
return mid;

Also, does your teacher expect you to use recursion?


Please note, an ordered array is a precondition of the binary search.
To fix your code you might write:

#include <iostream>
#include <algorithm>

bool search(int a[], size_t size, int item)
{
  int beg = 0;
  int end = size-1;

  while (true)
  {
    if ( (beg + 1) >=  end)  
      return (item == a[end] || item ==a[beg]);
    int mid = (beg+end)/2;
    if ( item == a[mid])
      return true; 
    else if ( item < a[mid] )
      end = mid;
    else  
      beg = mid;
  }
}
enum { max_size = 90 };
int main()
{

  int a[max_size];

  int item;
  std::cout << "Enter the item to search for ";
  std::cin >> item;

  size_t size;
  std::cout << "Enter the array size ";
  std::cin >> size;

  if ( size > max_size)
  {
    std::cerr << "wrong input\n";
    return -1;
  }

  std::cout << "Enter the array elements\n";

  for (size_t n = 0; n< size; ++n)
    std::cin >> a[n];

  std::sort( &a[0], &a[size]);

  bool found  = search( a, size, item);
  std::cout << "The item is " << (found ? "found\n" : "not found\n");
}




However, in all programs but exercises, I would use the firepower of the standard C++ library:

#include <iostream>
#include <algorithm>
#include <vector>

int main()
{
  int item;
  std::cout << "Enter the item to search for ";
  std::cin >> item;

  size_t size;
  std::cout << "Enter the array size ";
  std::cin >> size;

  std::cout << "Enter the array elements\n";
  std::vector<int> v;
  v.resize(size);

  for (auto & x : v)
    std::cin >> x;

  sort(v.begin(), v.end());

  auto found =  binary_search(v.begin(), v.end(), item);
  std::cout << "The item is " << (found ? "found\n" : "not found\n");

}


这篇关于预期'('功能样式演员或类型构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆