简体   繁体   English

C ++:使用类型名作为基础的模板类中的函数调用编译时错误

[英]C++ : Function call Compile-Time Error from a Template Class using Typename as Base

I don't know and unable to find the concept name to form my question simple (And also the title too). 我不知道,也找不到概念名称来简化我的问题(标题也是如此)。 So i posted the entire code. 所以我发布了整个代码。 Below is the code, with two base classes. 下面是具有两个基类的代码。 And one derived class created using class template. 然后使用类模板创建一个派生类。 Derive class inherits the two base class using typename T. Each base class are having unique function called foo() & boo(). 派生类使用类型名T继承两个基类。每个基类都具有称为foo()和boo()的唯一函数。

My questions are: 我的问题是:
1) In this condition, is it possible to use both the function call in the derived class (in callMethods())? 1)在这种情况下,是否可以在派生类中同时使用两个函数调用(在callMethods()中)? i am getting error and no idea to solve it. 我遇到错误,不知道要解决它。
2) Or do i need to change the class design if the existing design is wrong? 2)如果现有设计错误,还是需要更改类设计?
3) Is it a good practice to design the class like this if the design is correct? 3)如果设计正确,设计这样的班级是一种好习惯吗?

Error Message: 错误信息:

error: 'boo' is not a member of 'mybase_1'
         T::boo();     
error: 'foo' is not a member of 'mybase_2'
         T::foo();      
                ^                ^

Test code: 测试代码:

enum class myenum : int {one, two};

class mybase_1{
protected:
    void foo(){
        qDebug() << "foo called\n";
    }
};

class mybase_2{
protected:
    void boo(){
        qDebug() << "boo called\n";
    }
};

template <typename T>
class myderived : public T{
public:
    myderived(myenum _enm);
    void callMethods();
private:
    myenum enm;
};

template <typename T> myderived<T>::myderived(myenum _enm):enm{_enm}{
    qDebug() << "derived constructor\n";
}
template <typename T> void myderived<T>::callMethods(){
    switch(enm){
    case myenum::one:
        T::foo();
        break;
    case myenum::two:
        T::boo();
        break;
    }
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    myderived<mybase_1> tmp1(myenum::one);
    tmp1.callMethods();
    myderived<mybase_2> tmp2(myenum::two);
    tmp2.callMethods();

    return a.exec();
}

1) In this condition, is it possible to use both the function call in the derived class? 1)在这种情况下,是否可以在派生类中同时使用两个函数调用?

In your implementation, only if the derived or the base class implements both functions. 在您的实现中,仅当派生类或基类实现这两个函数时。

i am getting error and no idea to solve it. 我遇到错误,不知道要解决它。

The type is known at compile time, so the simplest solution is to specialize the template: 该类型在编译时是已知的,因此最简单的解决方案是专门化模板:

template<>
void myderived<mybase_1>::callMethods(){
    foo();
}
template<>
void myderived<mybase_2>::callMethods(){
    boo();
}

If you don't need the enum for anything else, then you can throw it away along with the switch statement. 如果您不需要枚举,则可以将其与switch语句一起丢弃。

1) Just to rectify your errors— Your switch case is 1)只是为了纠正您的错误-您的开关盒是

switch(enm){
case myenum::one:
    T::foo();
    break;
case myenum::two:
    T::boo();
    break;
}

which assumes that T has both foo() and boo(). 假设T同时具有foo()和boo()。 Define these two functions in both the base classes(it doesn't matter if they are empty.) 在两个基类中定义这两个函数(它们是否为空无关紧要。)

What I guess you are trying to do is call a function of base from derived type, which is inheritance has been created I guess. 我猜您想做的是从派生类型调用base函数,我猜是继承已创建。

enum class myenum : int {one, two};

class mybase_1{
protected:
    void foo(){
        qDebug() << "foo in mybase_1 is called \n";
    }
};

class mybase_2{
protected:
    void foo(){
        qDebug() << "foo in mybase_2 is called\n";
    }
};

template <typename T>
class myderived : public T{
public:
    myderived(myenum _enm);
    void callMethods();
private:
    myenum enm;
};

template <typename T> myderived<T>::myderived(myenum _enm):enm{_enm}{
    qDebug() << "derived constructor\n";
}
template <typename T> void myderived<T>::callMethods(){
    foo(); // the foo called depends on the parameter supplied to template, which determines from which class your derived class has been derived.
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    myderived<mybase_1> tmp1(myenum::one);
    tmp1.callMethods();
    myderived<mybase_2> tmp2(myenum::two);
    tmp2.callMethods();

    return a.exec();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在模板类型名上编译时条件? - How to compile-time condition on template typename? C ++中的编译时条件成员函数调用 - compile-time conditional member function call in C++ C ++隐藏编译时多态性的模板习惯用法 - C++ hide template idiom from compile-time polymorphism C ++线程错误 - 带有类参数的编译时错误 - C++ Thread Error - compile-time error with class argument 如果没有初始化类字段,为什么在c ++中没有编译时错误? - why in c++ there are no compile-time error if class field is not initialized? 在 c++ function 中计算斐波那契并抛出编译时错误 - Computing fibonacci in c++ function and throwing compile-time error 通过使用traits使用模板类在编译时抛出错误来禁用函数 - Disable a function by throwing error at compile-time with template class using traits 如何在 C/C++ 中使用原始数据(字节)在运行时(而不是强制转换为编译时定义的函数)通过给定指针调用 function - How to call function by given pointer using raw data (bytes) on runtime (instead of casting to compile-time defined function) in C/C++ C++ 中的编译时 Base64 解码 - Compile-Time Base64 Decoding in C++ 如何在 C++ 编译时设计具有可切换成员变量的模板类? - How to design a template class with switchable member variables at compile-time in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM