此刻只是有些沮丧。用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 &,const std :: fpos &) 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 ' 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 array1213 casino[1] = "Alpha"; //Fills array14 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";2324 /*Query user for search value*/2526 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;3031 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;3536 /*Call Search Function & Display of Result*/3738 search(casino, i);3940 cout<<" *** Good Bye! ***"<<endl;4142 return 0;43 }444546 int search (string casino[ ], int area)47 {48 string result;49 bool answer;50 answer=false;5152 while ((area <= 10) && (!answer))53 { //Check if number is in search area.54 if (casino[area] == area) //***BAD LINE***//55 answer=true;56 else57 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;6465 return 0;66 } (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 您的比较是在字符串和整数之间。没有为此定义的运算符重载,因此编译器会给出错误。也许你想要这个#include <iostream>#include <cstring>using namespace std;void search (string casino[ ], int casinolen, unsigned int area); //Prototypeint 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方法知道其自身的长度。 (adsbygoogle = window.adsbygoogle || []).push({}); 10-04 21:55