我正在使用 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;
}
我得到输出:
为什么在第二种情况下会出现这种奇怪的行为?