我正在为解析器编写“字符串拆分器”,并且希望能够同时使用char和wchar_t。我有以下方法:

static void tokenize(const basic_string<T> &, vector<CToken> &);

我想给它一个宽字符串来解析:
vector<CToken> tTokens;
CTokenizer<wstring>::tokenize(L"if(i == 0) { print i + 2; } else { return; }", tTokens);

我也尝试过:
vector<CToken> tTokens;
const wstring sCode = L"if(i == 0) { print i + 2; } else { return; }";
CTokenizer<wstring>::tokenize(sCode, tTokens);

所以我认为我对模板有些不了解。请问我该怎么办?

谢谢你的帮助 !

编辑:这是我的构建日志:
1>------ Build started: Project: c_parser, Configuration: Debug Win32 ------
1>  Parsercpp.cpp
1>c:\users\virus\documents\visual studio 2012\projects\c_parser\c_parser\parsercpp.cpp(44): error C2958: the left parenthesis '(' found at 'c:\users\virus\documents\visual studio 2012\projects\c_parser\c_parser\parsercpp.cpp(38)' was not matched correctly
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring(529): error C2621: member 'std::_String_val<_Val_types>::_Bxty::_Buf' of union 'std::_String_val<_Val_types>::_Bxty' has copy constructor
1>          with
1>          [
1>              _Val_types=std::_Simple_types<std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring(532) : see reference to class template instantiation 'std::_String_val<_Val_types>::_Bxty' being compiled
1>          with
1>          [
1>              _Val_types=std::_Simple_types<std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring(627) : see reference to class template instantiation 'std::_String_val<_Val_types>' being compiled
1>          with
1>          [
1>              _Val_types=std::_Simple_types<std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring(700) : see reference to class template instantiation 'std::_String_alloc<_Al_has_storage,_Alloc_types>' being compiled
1>          with
1>          [
1>              _Al_has_storage=false,
1>              _Alloc_types=std::_String_base_types<std::wstring,std::allocator<std::wstring>>
1>          ]
1>          c:\users\virus\documents\visual studio 2012\projects\c_parser\c_parser\parsercpp.cpp(104) : see reference to class template instantiation 'std::basic_string<_Elem>' being compiled
1>          with
1>          [
1>              _Elem=std::wstring
1>          ]
1>c:\users\virus\documents\visual studio 2012\projects\c_parser\c_parser\parsercpp.cpp(104): error C2664: 'nsParser::CTokenizer<T>::tokenize' : cannot convert parameter 1 from 'const std::wstring' to 'const std::basic_string<_Elem> &'
1>          with
1>          [
1>              T=std::wstring
1>          ]
1>          and
1>          [
1>              _Elem=std::wstring
1>          ]
1>          Reason: cannot convert from 'const std::wstring' to 'const std::basic_string<_Elem>'
1>          with
1>          [
1>              _Elem=std::wstring
1>          ]
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

编辑:
template<class T>
class CTokenizer
{
public:
    static vector<T> s_tKeywords;
public:
    static void tokenize(const basic_string<T> &, vector<CToken> &);
};

来自parser.cpp的代码是上面的代码:
vector<CToken> tTokens;
const wstring sCode = L"if(i == 0) { print i + 2; } else { return; }";
CTokenizer<wstring>::tokenize(sCode, tTokens);

最佳答案

您应该实例化CTokenizer<wchar_t>,而不是CTokenizer<std::wstring>。这样做的方式,tokenize()的签名变为

static void tokenize(const basic_string<wstring> &, vector<CToken> &);

08-25 17:04