1

パーサー用の「文字列スプリッター」を書いていますが、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);
4

2 に答える 2

1

CTokenizer<wchar_t>ではなく、インスタンス化する必要がありますCTokenizer<std::wstring>。あなたがそれをやっている方法は、の署名にtokenize()なります

static void tokenize(const basic_string<wstring> &, vector<CToken> &);
于 2013-06-15T11:03:29.773 に答える
0

tokenizeの宣言を次のように変更します。

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

またはへの呼び出し

CTokenizer<wchar_t>::tokenize(L"if(i == 0) { print i + 2; } else { return; }", tTokens);

コメントの後に編集します。次に、「または」の部分を使用します(「どちらか」ではなく)...

そしてT、「文字型」(文字列型ではない)であるため、名前を意味のあるものに変更することをお勧めします。

2 番目の EDIT のコードを取得すると、これらの変更後のクラスは次のようになります。

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>。)

于 2013-06-15T11:04:30.980 に答える