我正在为解析器编写一个“字符串拆分器”,我希望能够同时使用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);
答案 0 :(得分:1)
您应该实例化CTokenizer<wchar_t>
,而不是CTokenizer<std::wstring>
。你这样做的方式,tokenize()
的签名变为
static void tokenize(const basic_string<wstring> &, vector<CToken> &);
答案 1 :(得分:0)
将tokenize
的声明更改为
static void tokenize(const T &, vector<CToken> &);
或致电
CTokenizer<wchar_t>::tokenize(L"if(i == 0) { print i + 2; } else { return; }", tTokens);
编辑:然后使用“或”部分(而不是“或”)......
由于T
是“char类型”(不是字符串类型),我建议你将它重命名为有意义的东西。
获取您的第二个编辑的代码,在这些更改后,该类看起来像:
template<class CharT>
class CTokenizer
{
public:
static vector< basic_string<CharT> > s_tKeywords;
public:
static void tokenize(const basic_string<CharT> &, vector<CToken> &);
};
然后你按照我上面的说法(tokenize
而非wchar_t
作为模板类型参数调用wstring
。 (wstring
只是basic_string<wchar_t>
的类型别名。)