读书人

case标号的值是不是不能用STRING类的常

发布时间: 2012-03-03 15:33:03 作者: rapoo

case标号的值是不是不能用STRING类的常量?
如题,程序如下:
#include <iostream>
#include <string>
using namespace std;

int pri(string &s)
{
switch(s)
{
case "# ":
return 1;
case "&& ":
return 2;
case "|| ":
return 2;
case "> ":
return 3;
case " < ":
return 3;
case "> = ":
return 3;
case " <= ":
return 3;
case "!= ":
return 3;
case "== ":
return 3;
case "+ ":
return 4;
case "- ":
return 4;
case "* ":
return 5;
case "/ ":
return 5;
case "! ":
return 6;
}
}

bool operator> (string &s1,string &s2)
{
int i = pri(s1);
int j = pri(s2);
return (i> j);
}

bool operator <(string &s1,string &s2)
{
int i = pri(s1);
int j = pri(s2);
return (i <j);
}

bool operator==(string &s1,string &s2)
{
int i = pri(s1);
int j = pri(s2);
return (i==j);
}

int main ()
{
string a;
string b;
cin > > a;
cout < < "b: ";
cin > > b;
if(a> b)
cout < < "a> b " < < endl ;
else if(a <b)
cout < < "a <b " < < endl;
else cout < < "a==b " < < endl;
return 0;

}
错误有:
error C2051: case 表达式不是常量
“std::string”类型的 switch 表达式是非法的
没有可用于执行该转换的用户定义的转换运算符,或者无法调用该运算符

[解决办法]
if else if

读书人网 >C++

热点推荐