0

我正在使用 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;
}

我得到输出:

在此处输入图像描述

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

4

3 回答 3

5

operator+=之所以有效,是因为它是模板类实例化的非模板方法std::stringstd::string是模板实例化的 typedef std::basic_string)。它做了一些非常出乎意料的事情,将其int转换为 a 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_stringwith _CharT= char,而对于int,它只会匹配上面的签名 if _CharT= int

在您的情况下,您的标量char类型是int,而您std::string的是std::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+=为具有类似规则的模板函数来禁用。这可能不那么令人惊讶。但这不是标准所说的。

于 2013-04-11T20:24:37.280 回答
1

运算符 + 仅用于字符串-字符串连接,而运算符 += 不仅可以附加到现有字符串,还可以附加 char* 和 char。

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

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

于 2013-04-11T20:08:20.923 回答
0

不能加上 string(x="hello") 和 int(y) 类型。

于 2013-04-11T20:44:23.070 回答