简体   繁体   English

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

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

Just a little frustrated at the moment. 此刻只是有些沮丧。 Having fun as a noob with C++. 用C ++作为菜鸟玩得开心。 Using g++ compiler. 使用g ++编译器。 In the search function, I come up with several errors on line 54. Below are the first two, since I know this all has to do with one or two errors that I cannot see at the moment. 在搜索功能中,我在第54行出现了几个错误。下面是前两个错误,因为我知道所有这些都与目前看不到的一个或两个错误有关。

ghp1.cpp: In function 'int search(std::string*, int)': ghp1.cpp:在函数'int search(std :: string *,int)'中:
ghp1.cpp:54:22: error: no match for 'operator==' (operand types are 'std::string {aka std::basic_string}' and 'int') ghp1.cpp:54:22:错误:“ operator ==”不匹配(操作数类型为“ std :: string {aka std :: basic_string}”和“ int”)

 if (casino[area] == area) ^ 

ghp1.cpp:54:22: note: candidates are: ghp1.cpp:54:22:注意:候选人为:
In file included from /usr/local/lib/gcc48/include/c++/iosfwd:40:0, 在/ usr / local / lib / gcc48 / include / c ++ / iosfwd:40:0包含的文件中,
from /usr/local/lib/gcc48/include/c++/ios:38, 从/ usr / local / lib / gcc48 / include / c ++ / ios:38,
from /usr/local/lib/gcc48/include/c++/ostream:38, 来自/ usr / local / lib / gcc48 / include / c ++ / ostream:38,
from /usr/local/lib/gcc48/include/c++/iostream:39, 从/ usr / local / lib / gcc48 / include / c ++ / iostream:39,
from ghp1.cpp:7: 来自ghp1.cpp:7:
/usr/local/lib/gcc48/include/c++/bits/postypes.h:216:5: note: template bool std::operator==(const std::fpos<_StateT>&, const std::fpos<_StateT>&) /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: note: template argument deduction/substitution failed: /usr/local/lib/gcc48/include/c++/bits/postypes.h:216:5:注意:模板参数推导/替换失败:
ghp1.cpp:54:25: note: 'std::string {aka std::basic_string}' is not derived from 'const std::fpos<_StateT>' ghp1.cpp:54:25:注意:“ std :: string {aka std :: basic_string}”并非源自“ const std :: fpos <_StateT>”

 if (casino[area] == area) ^ 

Below is the code for my program which is supposed to make a search and display the result in a function (outside of main() ). 以下是我的程序的代码,该代码应该进行搜索并在函数中显示结果(在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  }

Your comparison is between a string and an integer. 您的比较是在字符串和整数之间。 There is no defined operator overload for this, so the compiler gives an error. 没有为此定义的运算符重载,因此编译器会给出错误。

Perhaps you want this 也许你想要这个

#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;
    }
}

You can input a number between 1 and 10 and it will print the name of the relevant casino. 您可以输入1到10之间的数字,它会打印相关娱乐场的名称。

It passes the length of the array (hardcoded here but could be obtained using sizeof ) to the search function to know if the number you have entered is within the bounds of the array. 它将数组的长度(此处是硬编码,但可以使用sizeof获得)传递给搜索函数,以了解您输入的数字是否在数组的范围内。 Its shifted by 1 as you want to input 1-10, whereas the array indices are actually 0-9. 当您要输入1-10时,它会移位1,而数组索引实际上是0-9。

Passing the length is an approach commonly used in C. For C++ you would do better use a vector , which know its own length using its size method. 传递长度是C语言中常用的一种方法。对于C ++,您最好使用vector ,该vector通过size方法知道自己的长度。

The problem is pretty simple actually. 这个问题实际上很简单。 The compiler is complaining about not "know" about any operator==(...) overload for comparing string and int : 编译器抱怨没有“知道”任何用于比较stringint operator==(...)重载:

casino[area] == area  // bad string

casino is an array of strings, so casino[area] is a string while area is an integer. casino是一个字符串数组,因此casino[area]是一个字符串,而area是一个整数。 You function search should be expecting a string as second parameter: 函数搜索应将字符串作为第二个参数:

int search (string casino[ ], string area)

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

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