3

eMbedded Visual C++ 4.0 (SP4) はテンプレート関数をサポートしていますか? Visual C++ 6.0 で正常に動作するコードをコンパイルしようとすると、エラーが発生します。

コンパイルするテンプレート関数は次のとおりです。

template<class NodeType>
NodeType* MyFunction()
{
    // ... do stuff
    return new NodeType("foo"); // return a new class instance of type NodeType
}

テンプレート機能の使用法:

MyClass *myOjb = MyFunction<MyClass>(); // this is causing an error

コンパイル エラー:

error C2275: 'MyClass' : illegal use of this type as an expression
                         see declaration of 'MyClass'
error C2059: syntax error : ')'

変更する必要があるコンパイラ スイッチはありますか? これをコンパイルするにはどうすればよいですか?

4

1 に答える 1

3

ここで答えを見つけました。

「テンプレート関数がクラスメンバーでない場合は正常に機能します。まあ、グローバル関数として残しておく必要があると思います。」

于 2011-05-24T13:42:09.440 に答える