问题描述
我使用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:在函数'int main()':
test.cpp:8:6:error:在'x + y'中没有匹配'operator +'
test.cpp:8:6:注意:候选项为:
在包含在/usr/include/c++/4.7/bits/stl_algobase中的文件中。 h:68:0,
来自/usr/include/c++/4.7/bits/char_traits.h:41,
来自/usr/include/c++/4.7/ios:41,
从/usr/include/c++/4.7/ostream:40,
从/usr/include/c++/4.7/iostream:40,
从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 :注意:模板参数推导/替换失败:
test.cpp:8:6:注意:不匹配的类型'const std :: reverse_iterator< _Iterator>'和'int'
在/ usr /include/c++/4.7/string:54:0,
来自/usr/include/c++/4.7/bits/locale_classes.h:42,
来自/usr/include/c++/4.7/bits /ios_base.h:43,
从/usr/include/c++/4.7/ios:43,
从/usr/include/c++/4.7/ostream:40,
从/ usr /include/c++/4.7/iostream:40,
从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:注意:模板参数推导/替换失败:
test.cpp:8:6:注意:不匹配的类型的const std :: basic_string< _CharT,_Traits,_Alloc> '
在/usr/include/c++/4.7/string:55:0中包含的文件中,
来自/usr/include/c++/4.7/bits/locale_classes.h:42,
从/usr/include/c++/4.7/bits/ios_base.h:43,
从/usr/include/c++/4.7/ios:43,
从/usr/include/c++/4.7/ ostream:40,
从/usr/include/c++/4.7/iostream:40,
从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 /usr/include/c++/4.7/bits/basic_string.tcc:694:5:note:template参数推导/替换失败:
test.cpp:8:6:注意:不匹配的类型'const _CharT *'和'std :: basic_string< char>'
在/ usr / include /从/usr/include/c++/4.7/bits/locale_classes.h:42,
来自/usr/include/c++/4.7/bits/ios_base的c ++ / 4.7 /字符串:55:0,
。 h:43,
从/usr/include/c++/4.7/ios:43,
从/usr/include/c++/4.7/ostream:40,
从/ usr / include / c ++ / 4.7 / iostream:40,
从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 /替换失败:
test.cpp:8:6:注意:不匹配的类型const std :: basic_string< _CharT,_Traits,_Alloc>'和'int'
在/ usr / include /c++/4.7/string:54:0,
来自/usr/include/c++/4.7/bits/locale_classes.h:42,
来自/usr/include/c++/4.7/bits/ios_base .h:43,
从/usr/include/c++/4.7/ios:43,
从/usr/include/c++/4.7/ostream:40,
从/ usr / include /c++/4.7/iostream:40,
从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参数推导/替换失败:
test.cpp:8:6:注意:不匹配的类型'const _CharT *'和'int'
在/usr/include/c++/4.7/string中包含的文件: 54:0,
来自/usr/include/c++/4.7/bits/locale_classes.h:42,
来自/usr/include/c++/4.7/bits/ios_base.h:43,
从/usr/include/c++/4.7/ios:43,
从/usr/include/c++/4.7/ostream:40,
从/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 /替换失败:
test.cpp:8:6:注意:为参数_CharT('char'和'int')推导出冲突类型
在/ usr / include / c ++ / 4.7 / bits / stl_algobase.h:68:0,
来自/usr/include/c++/4.7/bits/char_traits.h:41,
来自/usr/include/c++/4.7/ios: 41,
从/usr/include/c++/4.7/ostream:40,
从/usr/include/c++/4.7/iostream:40,
从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:注意:模板参数扣除/替换失败:
test.cpp:8:6:注意:不匹配的类型const __gnu_cxx :: __ normal_iterator< _Iterator,_Container>'和'int'
make:*** [test]错误1
b
#include< iostream>
using namespace std;
int main()
{
string x =hello;
int y = 1;
x + = y;
cout<< x;
return 0;
}
我得到输出:
>
为什么这个奇怪的行为在第二种情况下?
operator + =
是模板类实例化的非模板方法 std :: string
( std :: string
是一个typedef std :: basic_string
的模板实例化)。它做了非常意想不到的事情,将 int
转换为 char
,然后将其视为字符。 p>
运算符+
不起作用,因为对于自由运算符+
是:
template< class _CharT,class _Traits,class _Alloc>
/ pre>
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
)
在你的情况下, $ c> char 类型是
int
,而您的std :: string
code> std :: basic_string< char,/*...*/> - 和char
和int
不匹配!
您的模板spew中的特定错误在这里:
/ 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 /替换失败:
test.cpp:8:6:注意:推导出参数_CharT('char'和'int')的冲突类型
我们可以通过修改
运算符+ =
string + = int >转换为具有类似规则的模板函数。这可能不太令人惊讶。但这不是标准说的。I am using gcc 4.7.2
With the following code:
#include <iostream> using namespace std; int main() { string x="hello"; int y=1; x=x+y; cout<<x; return 0; }
I get error:
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
Whereas with this :
#include <iostream> using namespace std; int main() { string x="hello"; int y=1; x+=y; cout<<x; return 0; }
I get the output:
Why this strange behaviour in second case?
解决方案
operator+=
works because it is a non-template method of the template class instantiationstd::string
(std::string
is a typedef to a template instantiation ofstd::basic_string
). It does something exceedingly unexpected, which is cast thatint
to achar
, then treat it as a character.
operator+
doesn't work because the corresponding overload for the freeoperator+
is:template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+( const std::basic_string<_CharT, _Traits, _Alloc>&, _CharT )
which does type deduction on both the
string
type and on the scalarchar
type. Both of these type deductions have equal weight, and there is no consistent type deduction on both! (Forstd::string
, it is astd::basic_string
with_CharT
=char
, while forint
, it would only match the above signature if_CharT
=int
)In your case, your scalar
char
type isint
, while yourstd::string
is astd::basic_string< char, /*...*/ >
-- andchar
andint
do not match!The particular error in your template spew is here:
/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’)
We could disable
string += int
by changingoperator+=
into a template function with similar rules. This might be less surprising. But it isn't what the standard says.这篇关于+运算符在字符串和int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!