字符串和int上的+运算符

时间:2013-04-11 20:04:17

标签: c++ string gcc

我正在使用gcc 4.7.2

使用以下代码:

#include <iostream>
using namespace std;

int main()
{
    string x="hello";
    int y=1;
    x=x+y;
    cout<<x;
    return 0;
}

我收到错误:

g++     test.cpp   -o test
test.cpp: In function ‘int main()’:
test.cpp:8:6: error: no match for ‘operator+’ in ‘x + y’
test.cpp:8:6: note: candidates are:
In file included from /usr/include/c++/4.7/bits/stl_algobase.h:68:0,
                 from /usr/include/c++/4.7/bits/char_traits.h:41,
                 from /usr/include/c++/4.7/ios:41,
                 from /usr/include/c++/4.7/ostream:40,
                 from /usr/include/c++/4.7/iostream:40,
                 from test.cpp:1:
/usr/include/c++/4.7/bits/stl_iterator.h:335:5: note: template<class _Iterator> std::reverse_iterator<_Iterator> std::operator+(typename std::reverse_iterator<_Iterator>::difference_type, const std::reverse_iterator<_Iterator>&)
/usr/include/c++/4.7/bits/stl_iterator.h:335:5: note:   template argument deduction/substitution failed:
test.cpp:8:6: note:   mismatched types ‘const std::reverse_iterator<_Iterator>’ and ‘int’
In file included from /usr/include/c++/4.7/string:54:0,
                 from /usr/include/c++/4.7/bits/locale_classes.h:42,
                 from /usr/include/c++/4.7/bits/ios_base.h:43,
                 from /usr/include/c++/4.7/ios:43,
                 from /usr/include/c++/4.7/ostream:40,
                 from /usr/include/c++/4.7/iostream:40,
                 from test.cpp:1:
/usr/include/c++/4.7/bits/basic_string.h:2362:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.7/bits/basic_string.h:2362:5: note:   template argument deduction/substitution failed:
test.cpp:8:6: note:   mismatched types ‘const std::basic_string<_CharT, _Traits, _Alloc>’ and ‘int’
In file included from /usr/include/c++/4.7/string:55:0,
                 from /usr/include/c++/4.7/bits/locale_classes.h:42,
                 from /usr/include/c++/4.7/bits/ios_base.h:43,
                 from /usr/include/c++/4.7/ios:43,
                 from /usr/include/c++/4.7/ostream:40,
                 from /usr/include/c++/4.7/iostream:40,
                 from test.cpp:1:
/usr/include/c++/4.7/bits/basic_string.tcc:694:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.7/bits/basic_string.tcc:694:5: note:   template argument deduction/substitution failed:
test.cpp:8:6: note:   mismatched types ‘const _CharT*’ and ‘std::basic_string<char>’
In file included from /usr/include/c++/4.7/string:55:0,
                 from /usr/include/c++/4.7/bits/locale_classes.h:42,
                 from /usr/include/c++/4.7/bits/ios_base.h:43,
                 from /usr/include/c++/4.7/ios:43,
                 from /usr/include/c++/4.7/ostream:40,
                 from /usr/include/c++/4.7/iostream:40,
                 from test.cpp:1:
/usr/include/c++/4.7/bits/basic_string.tcc:710:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(_CharT, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.7/bits/basic_string.tcc:710:5: note:   template argument deduction/substitution failed:
test.cpp:8:6: note:   mismatched types ‘const std::basic_string<_CharT, _Traits, _Alloc>’ and ‘int’
In file included from /usr/include/c++/4.7/string:54:0,
                 from /usr/include/c++/4.7/bits/locale_classes.h:42,
                 from /usr/include/c++/4.7/bits/ios_base.h:43,
                 from /usr/include/c++/4.7/ios:43,
                 from /usr/include/c++/4.7/ostream:40,
                 from /usr/include/c++/4.7/iostream:40,
                 from test.cpp:1:
/usr/include/c++/4.7/bits/basic_string.h:2399:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
/usr/include/c++/4.7/bits/basic_string.h:2399:5: note:   template argument deduction/substitution failed:
test.cpp:8:6: note:   mismatched types ‘const _CharT*’ and ‘int’
In file included from /usr/include/c++/4.7/string:54:0,
                 from /usr/include/c++/4.7/bits/locale_classes.h:42,
                 from /usr/include/c++/4.7/bits/ios_base.h:43,
                 from /usr/include/c++/4.7/ios:43,
                 from /usr/include/c++/4.7/ostream:40,
                 from /usr/include/c++/4.7/iostream:40,
                 from test.cpp:1:
/usr/include/c++/4.7/bits/basic_string.h:2415:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, _CharT)
/usr/include/c++/4.7/bits/basic_string.h:2415:5: note:   template argument deduction/substitution failed:
test.cpp:8:6: note:   deduced conflicting types for parameter ‘_CharT’ (‘char’ and ‘int’)
In file included from /usr/include/c++/4.7/bits/stl_algobase.h:68:0,
                 from /usr/include/c++/4.7/bits/char_traits.h:41,
                 from /usr/include/c++/4.7/ios:41,
                 from /usr/include/c++/4.7/ostream:40,
                 from /usr/include/c++/4.7/iostream:40,
                 from test.cpp:1:
/usr/include/c++/4.7/bits/stl_iterator.h:904:5: note: template<class _Iterator, class _Container> __gnu_cxx::__normal_iterator<_Iterator, _Container> __gnu_cxx::operator+(typename __gnu_cxx::__normal_iterator<_Iterator, _Container>::difference_type, const __gnu_cxx::__normal_iterator<_Iterator, _Container>&)
/usr/include/c++/4.7/bits/stl_iterator.h:904:5: note:   template argument deduction/substitution failed:
test.cpp:8:6: note:   mismatched types ‘const __gnu_cxx::__normal_iterator<_Iterator, _Container>’ and ‘int’
make: *** [test] Error 1

鉴于此:

#include <iostream>
using namespace std;

int main()
{
    string x="hello";
    int y=1;
    x+=y;
    cout<<x;
    return 0;
}

我得到了输出:

enter image description here

为什么在第二种情况下这种奇怪的行为?

3 个答案:

答案 0 :(得分:5)

operator+=有效,因为它是模板类实例化std::string的非模板方法(std::stringstd::basic_string的模板实例化的typedef。它做了一些非常意外的事情,将int投射到char,然后将其视为角色。

operator+不起作用,因为免费operator+的相应重载是:

template<class _CharT, class _Traits, class _Alloc>
std::basic_string<_CharT, _Traits, _Alloc>
  std::operator+(
    const std::basic_string<_CharT, _Traits, _Alloc>&,
    _CharT
  )

在标量string类型的char类型上进行类型推导。这两种类型的扣除都具有相同的权重,并且两者都没有一致的类型推论! (对于std::string,它是std::basic_string _CharT = char,而对于int,它只会匹配上述签名_CharT = int

在您的情况下,您的标量char类型为int,而您的std::stringstd::basic_string< char, /*...*/ > - charint不匹配!

模板喷出的特定错误在这里:

/usr/include/c++/4.7/bits/basic_string.h:2415:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, _CharT)
/usr/include/c++/4.7/bits/basic_string.h:2415:5: note:   template argument  deduction/substitution failed:
test.cpp:8:6: note:   deduced conflicting types for parameter ‘_CharT’ (‘char’ and ‘int’)

我们可以通过将string += int更改为具有类似规则的模板函数来禁用operator+=。这可能不那么令人惊讶。但这并不是标准所说的。

答案 1 :(得分:1)

Operator +仅定义为字符串串联,而operator + =不仅仅附加到现有字符串字符串,还附加char *和char。

operator + =有一个重载版本,它获取char并将此char附加到字符串。 您的案例中的int将转换为char并附加到字符串。这就是你在输入结束时得到这个奇怪角色的方式。

您应该使用sstream将int追加到字符串中。看这里:http://www.cplusplus.com/reference/ostream/ostream/operator%3C%3C/

答案 2 :(得分:0)

不能加上字符串(x =“hello”)和int(y)类型。