【问题标题】:Error "no match for 'operator+" when concatenating strings连接字符串时出现错误“'operator+'不匹配”
【发布时间】:2021-04-06 08:42:54
【问题描述】:

我正在尝试使用此代码反转字符串中的单词:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    //_ _ the sky is blue

    string vec;
    getline(cin, vec);
    stack<string> str;

    string temp = "";
    string ans = "";

    for (int i = 0; i < vec.length(); i++)
    {
        if (vec.at(i) == ' ')
        {
            if (temp.length() > 0)
            {
                str.push(temp);
                temp = "";
            }
            else
            {
                temp = temp + vec.at(i);
            }
        }
    }

    //ans = ans + temp;

    while (!str.empty())
    {
        ans = ans + " " + str.pop();
    }

    if (ans.length() != 0 && ans.at(0) == ' ')
        ans = ans.substr(1);

    cout << ans << endl;
}

我在第 33 行收到此错误,告诉 "no match for 'operator+'"

我已附上相关截图:

请帮忙。

【问题讨论】:

  • 您是否阅读了错误信息?它告诉你出了什么问题。它甚至会告诉你出错的那一行。
  • str.pop(); 返回 void,错误消息字面意思是这样。
  • 提示:str.pop() 返回什么?
  • 请不要发布带有文字的图片(甚至是屏幕截图)。

标签: c++ string stack reverse operator-keyword


【解决方案1】:

pop()stack成员方法,返回类型为void,不返回string,因此不能打印,也不能和其他字符串拼接。

正如错误所示,您不能使用 + 运算符为这两种不同类型添加 voidstring(除非您通过重载 + 运算符使该选项可用),所以 ans = ans + " " + str.pop();错了。

你可以使用:

while (!str.empty())
{
    ans = ans + " " + str.top();
    str.pop();
}

正如top() 确实返回一个string 对象。


我应该指出,使用#include &lt;bits/stdc++.h&gt; is badusing namespace std is also not very good,但是将它们放在一起是一场等待发生的灾难。

【讨论】:

    【解决方案2】:

    容器适配器std::stack的方法pop的返回类型为void。所以这个说法

    ans= ans+" "+str.pop();
    

    不正确,编译器会报错。

    你需要写一些类似的东西

    while(!str.empty()){
        ans= ans+" "+ str.top();
        str.pop();
    }
    

    注意这个for循环

     for(int i=0 ; i<vec.length(); i++){
      if(vec.at(i)==' '){
       if(temp.length()>0){
        str.push(temp);
        temp = "";
       }
    
       else{
        temp = temp + vec.at(i);
       }
      }
    }
    

    有一个错误。如果存储在对象vec 中的字符串不以空格字符结尾,则字符串的最后一个单词不会被压入堆栈。

    看来您正在尝试执行以下操作。

    #include <iostream>
    #include <string>
    #include <stack>
    #include <cctype>
    
    int main() 
    {
        std::string s( "   Hello World   " );
        std::stack<std::string> st;
        
        std::cout << "\"" << s << "\"\n";
        
        for ( std::string::size_type i = 0; i < s.size(); )
        {
            std::string tmp;
            
            while ( i < s.size() && isspace( ( unsigned char )s[i] ) )
            {
                tmp += s[i++];
            }
            
            if ( !tmp.empty() ) 
            {
                st.push( tmp );
                tmp.clear();
            }
            
            while ( i < s.size() && !isspace( ( unsigned char )s[i] ) )
            {
                tmp += s[i++];
            }
            
            if ( !tmp.empty() )
            {
                st.push( tmp );
            }
        }
        
        std::string result;
        
        while ( !st.empty() )
        {
            result += st.top();
            st.pop();
        }
        
        std::cout << "\"" << result << "\"\n";
        
        return 0;
    }
    

    程序输出是

    "   Hello World   "
    "   World Hello   "
    

    【讨论】:

    • 其实我同意用str.top()代替str.pop()。但是,我的代码在 vec 上运行良好。
    • @RafiaChy 你错了如果像“1 2”这样的字符串不以空格结尾,那么单词“2”将不会被压入堆栈。
    • @RafiaChy 另一个问题是用户可以分隔字符串中的单词,例如使用制表符'\t'。在这种情况下,您的程序将无法运行。它也不会保留单词之间的空格数。
    【解决方案3】:

    谢谢你们帮助我。这是代码并且工作得很好:

    #include <bits/stdc++.h>
    
    using namespace std;
    
    int main(){
    
    //_ _ the sky is blue
    
    string vec;
    getline(cin, vec);
    stack<string>str;
    string rs;
    
    string temp="";
    string ans= "";
    
    for(int i=0 ; i<vec.length(); i++){
    if(vec.at(i)==' '){
       if(temp.length()>0){
        str.push(temp);
        temp = "";
       }
    }
       else{
        temp = temp + vec.at(i);
       }
    }
    
    
         ans = ans + temp;
    
        while(!str.empty()){
            ans= ans+" "+str.top();
            str.pop();
    
        }
    
        if(ans.length() != 0 && ans.at(0) == ' '){
          ans = ans.substr(1);
       }
    
    cout<<ans<<endl;
    reverse( ans.begin(), ans.end());
    cout<<ans<<endl;
    }
    

    这里的输出只允许每个单词之间有一个空格,并消除了前导和尾随空格:

    【讨论】:

    • 正如我在 cmets 中对我的回答所写的那样,您的程序不正确。重读我的答案,并接受它。
    猜你喜欢
    • 2012-05-13
    • 1970-01-01
    • 2021-09-12
    • 2011-10-06
    • 1970-01-01
    • 2020-10-11
    • 1970-01-01
    • 2014-05-16
    • 2013-06-05
    相关资源
    最近更新 更多