lucky3778 2022-06-20 20:44 采纳率: 81.8%
浏览 52
已结题

关于模板类继承,这个程序该怎么改不报错?



#include <iostream>
#include <string>
using namespace std;

class interface
{
public:
    bool say()
    {
        printf("interface ok\n");
        return true;
    }
};


template<typename gf>
class grandfather
{
private: 
    string mgf;
public:
    grandfather(string igf)
    {
        mgf = igf;
    }
    
    template<typename TResult>
    TResult CallFunction(TResult& func(gf))
    {
        gf cgf;
        TResult result;
        result = func(cgf);
        return result;
    }
};


template<typename f>
class father :public grandfather<f>
{
private: 
    string mf;
protected:
    father():grandfather<f>(mf)//顺便调用基类的有参构造
    {}
public:
    bool Fun_father_text(f cfa)
    {
        retrun cfa.father_text();
    }
    template<typename TResult>
    bool father_text()
    {
        bool result;
        result = this->CallFunction<bool>(Fun_father_text);
    }
};


class son :public father<interface>
{
    bool father<interface>::father_text();//结合这个
};

int main()
{
    son son1;
    if (son1.father_text())//应该怎么改才能让这个不报错
    {
        cout << "ture" << endl;
    }
    else
    {
        cout << "fase" << endl;
    }
    return 0;
}


上面这个是程序,就只需要不报错就行了,我改了son的声明方式,或者虚函数都没有不报错。其实就是想纠正一些我的知识错误。感谢

  • 写回答

3条回答 默认 最新

  • flower980323 2022-06-21 16:09
    关注

    建议买本C++Prime学习好语法,再写代码,下面这写的啥玩意

    class son :public father<interface>
    {
        bool father<interface>::father_text();//结合这个
    };
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 7月6日
  • 已采纳回答 6月28日
  • 修改了问题 6月20日
  • 创建了问题 6月20日