繁体   English   中英

错误:“ operator ==”不匹配,模板参数推导/替换失败:

[英]error: no match for 'operator==' and template argument deduction/substitution failed:

此刻只是有些沮丧。 用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 < _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 <_StateT>”

 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  }

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

也许你想要这个

#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 ++,您最好使用vector ,该vector通过size方法知道自己的长度。

这个问题实际上很简单。 编译器抱怨没有“知道”任何用于比较stringint operator==(...)重载:

casino[area] == area  // bad string

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

int search (string casino[ ], string area)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM