`std :: stringstream :: operator>>()`不能解除模板类型

时间:2015-12-26 11:44:15

标签: c++ templates stringstream

我尝试使用std::ostringstream类编写通用字符串解析器函数。

#include <iostream>
#include <sstream>
#include <string>

template <class ValueType, class CharType>
ValueType parse_string(const std::basic_string<CharType> & String)
{
    std::basic_ostringstream<CharType> oss(String);
    ValueType Value;
    oss >> Value;   // ERROR HERE!
    return Value;
}

int wmain(int argc, wchar_t *argv[]/*, wchar_t *envp[]*/)
{
    const std::string SString("3.14");
    std::cout << "SString : " << SString << std::endl;
    std::cout << "int     : " << parse_string<int>(SString) << std::endl;
    std::cout << "double  : " << parse_string<double>(SString) << std::endl;

    const std::wstring WString(L"2.718");
    std::wcout << L"WString : " << WString << std::endl;
    std::wcout << L"int     : " << parse_string<int>(WString) << std::endl;
    std::wcout << L"double  : " << parse_string<double>(WString) << std::endl;

    return 0;
}

我在标记的行中收到以下错误。

Error   C2676   binary '>>': 'std::basic_ostringstream<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>' does not define this operator or a conversion to a type acceptable to the predefined operator
Error   C2678   binary '>>': no operator found which takes a left-hand operand of type 'std::basic_ostringstream<char,std::char_traits<char>,std::allocator<char>>' (or there is no acceptable conversion)
Error   C2784   'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,_Elem *)': could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::basic_ostringstream<char,std::char_traits<char>,std::allocator<char>>'
Error   C2784   'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,_Elem &)': could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::basic_ostringstream<char,std::char_traits<char>,std::allocator<char>>'
Error   C2784   'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,signed char *)': could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::basic_ostringstream<char,std::char_traits<char>,std::allocator<char>>'
Error   C2784   'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,signed char &)': could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::basic_ostringstream<char,std::char_traits<char>,std::allocator<char>>'
Error   C2784   'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,unsigned char *)': could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::basic_ostringstream<char,std::char_traits<char>,std::allocator<char>>'
Error   C2784   'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,unsigned char &)': could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::basic_ostringstream<char,std::char_traits<char>,std::allocator<char>>'
Error   C2784   'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &&,_Ty &)': could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &&' from 'std::basic_ostringstream<char,std::char_traits<char>,std::allocator<char>>'
Error   C2784   'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &&,std::basic_string<_Elem,_Traits,_Alloc> &)': could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &&' from 'std::basic_ostringstream<char,std::char_traits<char>,std::allocator<char>>'
Error   C2784   'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)': could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::basic_ostringstream<char,std::char_traits<char>,std::allocator<char>>'
Error   C2676   binary '>>': 'std::basic_ostringstream<char,std::char_traits<char>,std::allocator<char>>' does not define this operator or a conversion to a type acceptable to the predefined operator
Error   C2678   binary '>>': no operator found which takes a left-hand operand of type 'std::basic_ostringstream<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>' (or there is no acceptable conversion)
Error   C2784   'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,_Elem *)': could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::basic_ostringstream<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>'
Error   C2784   'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,_Elem &)': could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::basic_ostringstream<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>'
Error   C2784   'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,signed char *)': could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::basic_ostringstream<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>'
Error   C2784   'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,signed char &)': could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::basic_ostringstream<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>'
Error   C2784   'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,unsigned char *)': could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::basic_ostringstream<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>'
Error   C2784   'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,unsigned char &)': could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::basic_ostringstream<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>'
Error   C2784   'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &&,_Ty &)': could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &&' from 'std::basic_ostringstream<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>'
Error   C2784   'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &&,std::basic_string<_Elem,_Traits,_Alloc> &)': could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &&' from 'std::basic_ostringstream<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>'
Error   C2784   'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)': could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::basic_ostringstream<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>'

我的代码有什么问题?

1 个答案:

答案 0 :(得分:4)

basic_ostringstream有运营商<<而不是>> http://en.cppreference.com/w/cpp/io/basic_ostringstream