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日

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?