错误:不匹配'运营商=='和模板参数推断/替换失败:

时间:2015-02-17 22:58:02

标签: c++ function while-loop

此刻有点沮丧。用C ++作为菜鸟玩得开心。使用g ++编译器。在搜索功能中,我在第54行提出了几个错误。以下是前两个错误,因为我知道这一切都与我目前看不到的一两个错误有关。

  

ghp1.cpp:在函数'int search(std :: string *,int)'中:
  ghp1.cpp:54:22:错误:'operator =='不匹配(操作数类型为'std :: string {aka std :: basic_string}'和'int')

     
if (casino[area] == area)
                 ^
  
     

ghp1.cpp:54:22:注意:候选人是:
  在/ usr / local / lib / gcc48 / include / c ++ / iosfwd:40:0中包含的文件中,
                   来自/ usr / local / lib / gcc48 / include / c ++ / ios:38,
                   来自/ usr / local / lib / gcc48 / include / c ++ / ostream:38,
                   来自/ usr / local / lib / gcc48 / include / c ++ / iostream:39,
                   来自ghp1.cpp:7:
  /usr/local/lib/gcc48/include/c++/bits/postypes.h:216:5:注意:模板bool std :: operator ==(const std :: fpos< _StateT>&,const std :: fpos&lt ; _StateT>&)

     
operator==(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
^
  
     

/ usr / local / lib / gcc48 / include / c ++ / bits / postypes.h:216:5:注意:模板参数扣除/替换失败:
  ghp1.cpp:54:25:注意:'std :: string {aka std :: basic_string}'不是来自'const std :: fpos&lt; _StateT&gt;'

     
if (casino[area] == area)
                    ^
  

下面是我的程序的代码,它应该在一个函数中进行搜索并显示结果(在main()之外)。

 1  #include <iostream>
 2  #include <cstring>
 3
 4  using namespace std;
 5
 6  int search (string casino[ ], int area);        //Prototype
 7
 8  int main(void)
 9  {
10  int i=0;
11  string casino[11];                              //Creates array
12
13  casino[1] = "Alpha";                            //Fills array
14  casino[2] = "Bravo";
15  casino[3] = "Charlie";
16  casino[4] = "Delta";
17  casino[5] = "Echo";
18  casino[6] = "Foxtrot";
19  casino[7] = "Golf";
20  casino[8] = "Hotel";
21  casino[9] = "India";
22  casino[10] = "Juno";
23
24  /*Query user for search value*/
25
26  cout<<"     This program will help you find the first ten gaming zones of ";
27  cout<<"a casino."<<endl;
28  cout<<"Pick a number between 1 and 10: "<<endl;
29  cin>>i;
30
31  cout<<"     This program will help you find the first ten gaming zones of ";
32  cout<<"a casino."<<endl;
33  cout<<"Pick a number between 1 and 10: "<<endl;
34  cin>>i;
35
36  /*Call Search Function & Display of Result*/
37
38  search(casino, i);
39
40  cout<<"    *** Good Bye! ***"<<endl;
41
42  return 0;
43  }
44
45
46  int search (string casino[ ], int area)
47  {
48  string result;
49  bool answer;
50  answer=false;
51 
52  while ((area <= 10) && (!answer))
53     {                               //Check if number is in search area.
54     if (casino[area] == area)       //***BAD LINE***//
55         answer=true;
56     else
57         area++;
58     }
59  if (answer)
60     cout<<" That zone is named: "<<casino[area]<<endl;
61  else                                 //Display of incorrect input.
62     cout<<" Nice try smartie pants!"<<endl;
63     cout<<" I said, between 1 and 10."<<endl;
64 
65  return 0;
66  }

2 个答案:

答案 0 :(得分:1)

您的比较是在字符串和整数之间。没有为此定义的运算符重载,因此编译器会给出错误。

也许你想要这个

#include <iostream>
#include <cstring>

using namespace std;

void search (string casino[ ], int casinolen, unsigned int area);        //Prototype

int main(int, char**)
{
    unsigned int i=0;
    string casino[10];                              //Creates array

    casino[0] = "Alpha";                            //Fills array
    casino[1] = "Bravo";
    casino[2] = "Charlie";
    casino[3] = "Delta";
    casino[4] = "Echo";
    casino[5] = "Foxtrot";
    casino[6] = "Golf";
    casino[7] = "Hotel";
    casino[8] = "India";
    casino[9] = "Juno";

    /*Query user for search value*/

    cout<<"     This program will help you find the first ten gaming zones of ";
    cout<<"a casino."<< endl;
    cout<<"Pick a number between 1 and 10: "<<endl;
    cin>>i;

    /*Call Search Function & Display of Result*/

    search(casino, 10, i);

    cout << "    *** Good Bye! ***" << endl;

    return 0;
}

void search (string casino[ ], int casinolen, unsigned int area)
{
    if (area > 0 && area <= casinolen)
    {
        cout<<" That zone is named: "<<casino[area-1]<<endl;
    }
    else
    {//Display of incorrect input.
        cout<<" Nice try smartie pants!"<<endl;
        cout<<" I said, between 1 and " << casinolen << "."<<endl;
    }
}

您可以输入1到10之间的数字,它将打印相关赌场的名称。

它将数组的长度(此处硬编码但可以使用sizeof获得)传递给搜索函数,以了解您输入的数字是否在数组的范围内。当你想输入1-10时,它移1,而数组索引实际上是0-9。

传递长度是C中常用的方法。对于C ++,最好使用vectorsize使用{{1}}方法知道自己的长度。

答案 1 :(得分:0)

问题实际上非常简单。编译器抱怨没有“知道”用于比较operator==(...)string的任何int重载:

casino[area] == area  // bad string

casino是一个字符串数组,因此casino[area]是一个字符串,而area是一个整数。函数搜索应该期望一个字符串作为第二个参数:

int search (string casino[ ], string area)