从通用外部类返回指向嵌套内部类的指针

发布于 2024-08-27 17:28:33 字数 1286 浏览 5 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猥︴琐丶欲为 2024-09-03 17:28:33

当“A”被模板化时,编译器不够聪明,无法识别出“B”是一种类型。尝试使用类型名。

template <class E>
typename A<E>::B * A<E>::getB() {
    return new B();
}

The compiler isn't smart enough to figure that "B" is a type when "A" is templated. Try using typename.

template <class E>
typename A<E>::B * A<E>::getB() {
    return new B();
}
梦里人 2024-09-03 17:28:33

您需要在定义中使用 typename 来提示编译器 B 是一种类型。

template <class E>
typename A<E>::B * A::getB() {
   return new B;
}

You need to use typename in your definition to hint to the compiler that B is a type.

template <class E>
typename A<E>::B * A::getB() {
   return new B;
}
∝单色的世界 2024-09-03 17:28:33

对更新的回答:

您不需要在 C++ 中new所有内容,事实上,最好不要这样做,因为那样您就必须显式delete分配的内存或使用智能指针。

因此,这里是修改后的代码:

template <class E>
class A {

public:
    class B {
    public:
        int data;
    };

    B getB(); // Object, not pointer
};

template <class E>
typename A<E>::B A<E>::getB() {
    return B();
}

#include "A.h"

int main(int argc, char *argv[])
{
    A<C> a = A<C>();
    A<C>::B b = a.getB();
}

如果您希望new A类,那么您需要使用operator-> > 调用方法:

A<C>::B b = a->getB();

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 explicitly delete the memory allocated or use smart pointers.

So, here is your code revised:

template <class E>
class A {

public:
    class B {
    public:
        int data;
    };

    B getB(); // Object, not pointer
};

template <class E>
typename A<E>::B A<E>::getB() {
    return B();
}

#include "A.h"

int main(int argc, char *argv[])
{
    A<C> a = A<C>();
    A<C>::B b = a.getB();
}

If you wish to new the A<C> class, then you need to use the operator-> to invoke methods:

A<C>::B b = a->getB();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文