从通用外部类返回指向嵌套内部类的指针
我是 C++ 新手,所以请耐心等待。我有一个名为 A 的泛型类。A 有一个名为 B 的嵌套类。A 包含一个名为 getB() 的方法,该方法应该返回 B 的新实例。但是,我无法编译我的代码。它看起来像这样:#include
Ah
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 中使用它时,我收到错误。这是我的主要方法:
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>*'
再次感谢您的快速回复。
螺旋状
I'm new to C++, so bear with me. I have a generic class called A. A has a nested class called B. A contains a method called getB(), which is supposed to return a new instance of B. However, I can't get my code to compile. Here's what it looks like:#include
A.h
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();
}
When I try to compile this, I get the following error:
error: expected constructor, destructor, or type conversion before '*' token
Does anybody know what I'm doing wrong?
Thanks,
helixed
UPDATE:
Thanks for the quick replies everyone. I'm still having a little trouble getting this working. After taking the suggestions listed here, I have something like this:
A.h
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 {
};
However, when I try to use this from main, I get an error. Here's my main method:
main.cpp
#include "A.h"
int main(int argc, char *argv[])
{
A<C> *a = new A<C>();
A<C>::B *b = a.getB();
}
When I try to compile this, I get the following error:
error: request for member 'getB' in 'a', which is of non-class type 'A<C>*'
Thanks again for the quick responses.
helixed
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当“A”被模板化时,编译器不够聪明,无法识别出“B”是一种类型。尝试使用类型名。
The compiler isn't smart enough to figure that "B" is a type when "A" is templated. Try using typename.
您需要在定义中使用
typename
来提示编译器 B 是一种类型。You need to use
typename
in your definition to hint to the compiler that B is a type.对更新的回答:
您不需要在 C++ 中
new
所有内容,事实上,最好不要这样做,因为那样您就必须显式delete
分配的内存或使用智能指针。因此,这里是修改后的代码:
如果您希望类,那么您需要使用
new
Aoperator->
> 调用方法:Answer to the update:
You don't need to
new
everything in C++, in fact, it would be best if you did not, since then you would have to explicitlydelete
the memory allocated or use smart pointers.So, here is your code revised:
If you wish to
new
theA<C>
class, then you need to use theoperator->
to invoke methods: