它在if条件下给出了错误 [英] It is giving error on the if condition

查看:98
本文介绍了它在if条件下给出了错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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 a std::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 of std::string from something like iostream while operator== is defined in string.


The error message tells you the compiler tries to find a match for the comparison operator with the operand types std::string (obviously referring to Token.CP) and const char[9] (referring to the string literal, "Datatype").

This operator can be found in the header file string, 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 of std::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 try std::string::compare as suggested in solution 2 (although I think that will still leave you with some kind of problem elsewhere)


这篇关于它在if条件下给出了错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆