私はC++が初めてなので、ご容赦ください。A というジェネリック クラスがあります。A には B というネストされたクラスがあります。A には、B の新しいインスタンスを返すことになっている getB() というメソッドが含まれています。しかし、コードをコンパイルできません。これは次のようになります。
ああ
template <class E>
class A {
public:
class B {
public:
int data;
};
B * getB();
};
A.cpp
#include "A.h"
template <class E>
A<E>::B * A::getB() {
return new B();
}
これをコンパイルしようとすると、次のエラーが発生します。
error: expected constructor, destructor, or type conversion before '*' token
私が間違っていることを誰かが知っていますか?
ありがとう、
螺旋状
アップデート:
迅速な返信ありがとうございます。私はまだこれを機能させるのに少し苦労しています。ここにリストされている提案を行った後、次のようなものがあります。
ああ
template <class E>
class A {
public:
class B {
public:
int data;
};
B * getB();
};
template <class E>
typename A<E>::B * A<E>::getB() {
return new B();
}
class C {
};
ただし、これをメインから使用しようとすると、エラーが発生します。これが私の主な方法です:
main.cpp
#include "A.h"
int main(int argc, char *argv[])
{
A<C> *a = new A<C>();
A<C>::B *b = a.getB();
}
これをコンパイルしようとすると、次のエラーが発生します。
error: request for member 'getB' in 'a', which is of non-class type 'A<C>*'
迅速な対応に感謝します。
螺旋状