c++ - + 字符串和整数运算符

标签 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

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

最佳答案

operator+=之所以有效,是因为它是模板类实例化的非模板方法 std::string ( std::stringstd::basic_string 的模板实例化的类型定义)。它做了一些非常出乎意料的事情,即 intchar , 然后把它当作一个字符。

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 上输入 and类型。这两种类型推导具有相同的权重,并且两者没有一致的类型推导! (对于 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+=进入具有类似规则的模板函数。这可能并不令人惊讶。但这不是标准所说的。

关于c++ - + 字符串和整数运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15957860/

相关文章:

c# - 根据格式从字符串中获取值

c++ - 字符串不以 null 结尾

c++ - 如何使用函数列表指向 vector 对象中的成员函数?

c++ - 为什么 swap() 有时会通过传递数组来实现?

c++ - STL:在海量数据中进行排序和搜索

c++ - 指针(*)和引用(&)的区别

r - 在 data.frame 中查找字符串

linux - 如何找到 GCC 支持的所有 EABI 版本以及如何找到目标系统的 EABI 版本?

gcc - gcc中的-lm是什么意思?

c++ - std::atomic<X>::value_type 发生了什么?