使用模板变量访问基类的结构

发布于 2025-01-17 07:08:21 字数 708 浏览 3 评论 0原文

我想创建一个具有模板的基类的派生类并保留模板选项

,但我无法访问属于基类一部分的结构,

这是一个示例:

template <typename T1>
class Base{
    public:
    Base(){}
    ~Base(){}

    struct Struct {
        int Integer=0;
    };

    virtual void Function(Struct NewStrcut){ }

    //void Proccess(int T_Var){
    void Proccess(T1 T_Var){
        // do something
    }

};

template <typename T>
class Derived : public Base<T>{
//class Derived : public Base{
    Derived(){}
    ~Derived(){}

    void Function(Struct NewStrcut){
        // do something
    }

};

我收到以下错误:

错误:“结构”尚未声明 [​​build] 51 |空白 函数(结构NewStrcut){

我在 Ubuntu18 上使用 c++17 和 GCC11

I want to create a derived class of base class that has a template and to keep the template option

but I cannot access a struct that is part of the base class

here is an example:

template <typename T1>
class Base{
    public:
    Base(){}
    ~Base(){}

    struct Struct {
        int Integer=0;
    };

    virtual void Function(Struct NewStrcut){ }

    //void Proccess(int T_Var){
    void Proccess(T1 T_Var){
        // do something
    }

};

template <typename T>
class Derived : public Base<T>{
//class Derived : public Base{
    Derived(){}
    ~Derived(){}

    void Function(Struct NewStrcut){
        // do something
    }

};

I get the following error:

error: ‘Struct’ has not been declared [build] 51 | void
Function(Struct NewStrcut){

I am using c++17 with GCC11 on Ubuntu18

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

于我来说 2025-01-24 07:08:21

多亏了 @πάνταῥεῖ@fareanor,我发现一个答案

只需要添加“ TypeName base ::”在struct之前

,请在以下代码中看到:

template <typename T1>
class Base{
    public:
    Base(){}
    ~Base(){}

    struct Struct {
        int Integer=0;
    };

    virtual void Function(Struct NewStrcut){ }

    //void Proccess(int T_Var){
    void Proccess(T1 T_Var){
        // do something
    }

};

template <typename T>
class Derived : public Base<T>{
//class Derived : public Base{
    Derived(){}
    ~Derived(){}

    void Function( typename Base<T>::Struct NewStrcut){
        // do something
    }

};

thanks to @πάνταῥεῖ @Fareanor I found an answer

just needed to add "typename Base::" before Struct

as you can see in the following code:

template <typename T1>
class Base{
    public:
    Base(){}
    ~Base(){}

    struct Struct {
        int Integer=0;
    };

    virtual void Function(Struct NewStrcut){ }

    //void Proccess(int T_Var){
    void Proccess(T1 T_Var){
        // do something
    }

};

template <typename T>
class Derived : public Base<T>{
//class Derived : public Base{
    Derived(){}
    ~Derived(){}

    void Function( typename Base<T>::Struct NewStrcut){
        // do something
    }

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