wstring -> basic_string-6ren"> wstring -> basic_string-我正在为解析器编写一个“字符串拆分器”,我希望能够同时使用 char 和 wchar_t。我有以下方法: static void tokenize(const basic_string &, vect-6ren">
gpt4 book ai didi

C++ L"whatever"-> wstring -> basic_string

转载 作者:太空宇宙 更新时间:2023-11-04 11:53:27 24 4
gpt4 key购买 nike

我正在为解析器编写一个“字符串拆分器”,我希望能够同时使用 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> &);

关于C++ L"whatever"-> wstring -> basic_string<T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17122774/

24 4 0