error: no match for 'operator==' and template argument deduction/substitution failed:
error: no match for 'operator==' and template argument deduction/substitution failed:
此刻只是有点沮丧。作为 C++ 菜鸟的乐趣。使用 g++ 编译器。在搜索功能中,我在第 54 行发现了几个错误。下面是前两个,因为我知道这都与一两个我目前看不到的错误有关。
ghp1.cpp: In function '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')
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: 注意:模板参数 deduction/substitution 失败:
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 }
其实问题很简单。编译器抱怨 not "know" about any operator==(...)
overload for comparing string
and int
:
casino[area] == area // bad string
casino
是一个字符串数组,所以 casino[area]
是一个字符串,而 area
是一个整数。您的函数搜索应该期待一个字符串作为第二个参数:
int search (string casino[ ], string area)
您比较的是字符串和整数。没有为此定义运算符重载,因此编译器报错。
也许你想要这个
#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
,它使用其 size
方法知道自己的长度。
此刻只是有点沮丧。作为 C++ 菜鸟的乐趣。使用 g++ 编译器。在搜索功能中,我在第 54 行发现了几个错误。下面是前两个,因为我知道这都与一两个我目前看不到的错误有关。
ghp1.cpp: In function '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')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: 注意:模板参数 deduction/substitution 失败:
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 }
其实问题很简单。编译器抱怨 not "know" about any operator==(...)
overload for comparing string
and int
:
casino[area] == area // bad string
casino
是一个字符串数组,所以 casino[area]
是一个字符串,而 area
是一个整数。您的函数搜索应该期待一个字符串作为第二个参数:
int search (string casino[ ], string area)
您比较的是字符串和整数。没有为此定义运算符重载,因此编译器报错。
也许你想要这个
#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
,它使用其 size
方法知道自己的长度。