C++ STL用string類的時候 編譯器提示 error: no viable overloaded ‘=’,等於號不能重載//周末自己研究研究


C++ STL用string類的時候 編譯器提示 error: no viable overloaded ‘=’,等於號不能重載

關鍵代碼就是那句賦值

1
2
3
4
5
6
7
    ...

  if(s1_len > s2_len)
    for(int i = 1; i <= s1_len - s2_len; i++)
      s2 = '0' + s2;

    ...

然后編譯器報錯:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
main.cpp:41:10: error: no viable overloaded '='
      s2 = s2 + "0";
      ~~ ^ ~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:1330:19: note:
      candidate function not viable: 'this' argument has type 'const string' (aka 'const basic_string<char,
      char_traits<char>, allocator<char> >'), but method is not marked const
    basic_string& operator=(const basic_string& __str);
                  ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:1337:45: note:
      candidate function not viable: 'this' argument has type 'const string' (aka 'const basic_string<char,
      char_traits<char>, allocator<char> >'), but method is not marked const
    _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
                                            ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:1338:19: note:
      candidate function not viable: 'this' argument has type 'const string' (aka 'const basic_string<char,
      char_traits<char>, allocator<char> >'), but method is not marked const
    basic_string& operator=(value_type __c);
                  ^

我用的是Xcode的terminal-tools里的g++編譯


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM