它在if条件下给出了错误 [英] It is giving error on the if condition
问题描述
bool P()
{
int j = 0 ;
if (Token.CP == 数据类型)
{
return true ;
}
}
它不是比较在struct和dataype中的字符串类型Token.cp,也是一个字符串请帮助
错误是
2没有运算符==匹配这些操作数
操作数类型是:std :: string == const char [9]
试试这个
bool P ()
{
if (Token.CP.compare( 数据类型)== 0 )
{
返回 true ;
}
返回 false ;
}
如果Token.CP真的是std :: string
那么该行应该干净地编译。您是否在翻译单元中包含了std :: string的标题?如果不是,你的代码可能会从iostream
这样的东西中获取std :: string
的前向声明,而operator ==
在字符串
中定义。
错误消息告诉您编译器尝试使用操作数类型std :: string
查找比较运算符的匹配项(显然是指Token.CP
)和const char [9]
(指字符串文字,数据类型
)。
此运算符可以在头文件string
中找到,其中还包括类的std :: string
。请参阅 http://www.cplusplus.com/reference/string/string/operators/ [< a href =http://www.cplusplus.com/reference/string/string/operators/target =_ blanktitle =New Window> ^ ]如果你确实包含了字符串,你就不应该'得到那个错误。如果你没有包含字符串,你应该得到更多的错误,表明编译器不知道std :: string
的定义。
在后一种情况下,只需添加包含。
#include < string < span class =code-keyword>>
在前一种情况下,我很难相信你没有得到任何其他错误 - 最有可能的错误只是您需要先修复的另一个错误的结果。但为了安全起见,您可以按照解决方案2中的建议尝试std :: string :: compare
(尽管我认为这仍然会让您在其他地方遇到某种问题)
bool P()
{
int j=0;
if(Token.CP=="Datatype")
{
return true;
}
}
it is not comparing the Token.cp which is string type in struct and dataype which is also a string please help
error is
2 no operator "==" matches these operands
operand types are: std::string == const char [9]
Try this
bool P() { if(Token.CP.compare("Datatype")==0) { return true; } return false; }
If Token.CP is really astd::string
then that line should compile cleanly. Have you include the header for std::string in the translation unit? If not your code might be picking up a forward declaration ofstd::string
from something likeiostream
whileoperator==
is defined instring
.
The error message tells you the compiler tries to find a match for the comparison operator with the operand typesstd::string
(obviously referring toToken.CP
) andconst char[9]
(referring to the string literal,"Datatype"
).
This operator can be found in the header filestring
, which also includes the class,std::string
. See http://www.cplusplus.com/reference/string/string/operators/[^] If you did include string, you shouldn't get that error. If you didn't include string, you should get a lot more errors indicating that the compiler doesn't know the definition ofstd::string
.
In that latter case, just add the include.
#include <string>
In the former case, I have a hard time believing you don't get any other error - most likely the error is just a consequence of another that you need to fix first. But to be safe, you could trystd::string::compare
as suggested in solution 2 (although I think that will still leave you with some kind of problem elsewhere)
这篇关于它在if条件下给出了错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!