天天看点

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;

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:

    _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:

    basic_string& operator=(value_type __c);

继续阅读