【问题标题】:Getting error when comparing a character with component of a string in C++将字符与 C++ 中的字符串组件进行比较时出错
【发布时间】:2014-05-01 06:56:03
【问题描述】:
#include <iostream>
#include <fstream>
#include <cstring>
#define MAX_CHARS_PER_LINE 512
#define MAX_TOKENS_PER_LINE 20
#define DELIMITER " "
using namespace std;
int main ()
{
    //char buf[MAX_CHARS_PER_LINE];
    string buf; // string to store a line in file
    fstream fin;
    ofstream fout;
    fin.open("PiCalculator.h", ios::in);
    fout.open("op1.txt", ios::out);
    fout<< "#include \"PiCalculator.h\"\n";
    static int flag=0; //this variable counts the no of curly brackets

    while (!fin.eof())
    {
        // read an entire line into memory
        getline(fin, buf);
        //fout<<buf.back()<<endl;
        if(buf.back()== "{" && buf.front() !='c'){
            flag++;
            fout<<buf<<endl;
        }
        if(flag > 0)
            fout<<buf<<endl;
        if(buf.back()== "}"){
            flag--;
            fout<<buf<<endl;
        }
    }
    cout<<buf.back()<<endl;
    return 0;
}

这里我在 if 条件中遇到错误:

if(buf.back()== "{" && buf.front() !='c')

错误说明:ISO C++禁止指针和整数比较[-fpermissive]

谁能帮我解决问题??

【问题讨论】:

  • 首先需要#include &lt;string&gt;。其次,你需要阅读some documentation on std::string
  • 第三,这个:while (!fin.eof())is wrong
  • 第四,在 C++ 中使用预处理器 #defines 几乎从来都不是好事。阅读 const 和 (C++11) constexpr
  • @WhozCraig:为什么 while(!fin.eof()) 是错误的?
  • @cryptoaniac60 位于评论中提供的链接中。

标签: c++ string file


【解决方案1】:

由于您检查 bug.back 的“{”和 bug.front 的“c”,您的比较可能无效。我假设 back 和 front 函数返回的类型必须相同。一个是单引号,另一个“{”是双引号。这就是问题所在。

将“{”改为“{”

希望这会有所帮助。

【讨论】:

  • 这对我有帮助.. 非常感谢.. :)
【解决方案2】:

正如 Samuel 所说,std::string::back 返回一个 char&; (例如,请参阅http://en.cppreference.com/w/cpp/string/basic_string/back)并且您将其与字符串文字进行比较(双引号表示字符串,单引号表示字符)。

不清楚为什么在这种情况下不需要#include &lt;string&gt; 指令,但最好的做法是拥有它。不过,您需要&lt;cstring&gt; 并不明显。

我建议编译时打开警告;如果我以这种方式编译您的代码,我会收到相当有用的错误消息:

g++ -Wall -Wextra -std=c++11    q3.cc   -o q3
q3.cc: In function ‘int main()’:
q3.cc:24:25: warning: comparison with string literal results in unspecified behaviour [-Waddress]
         if(buf.back()== "{" && buf.front() !='c'){
                         ^
q3.cc:24:25: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
q3.cc:30:25: warning: comparison with string literal results in unspecified behaviour [-Waddress]
         if(buf.back()== "}"){
                         ^
q3.cc:30:25: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]

这是 gcc 版本 4.8.2。

【讨论】:

    【解决方案3】:

    buf.back() 返回 char 或 char &。

    您要比较的是 char 与字符串文字“{”,这是行不通的。

    将此行更改为(以及与字符串文字进行比较的所有其他行:

     if(buf.back()== '{' && buf.front() !='c'){
    

    注意'{'

    【讨论】:

      【解决方案4】:
      buf.back()== "{"
      

      字符串文字"{" 衰减为一个指针,buf.back() 返回一个int,因此你得到错误

      请改成

      buf.back() == '{'
      

      【讨论】:

        猜你喜欢
        • 2016-10-17
        • 2020-04-15
        • 1970-01-01
        • 2013-08-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-01
        • 1970-01-01
        相关资源
        最近更新 更多