gpt4 book ai didi

C++模板类继承

转载 作者:搜寻专家 更新时间:2023-10-31 00:10:38 24 4
gpt4 key购买 nike

#include <iostream>
using namespace std;

template <class T>
class C1
{
public:
int n;
C1(int a)
{
n=a;
}
T mat[50][50];
void readmat()
{
int i,j;
for(i=1; i<=n; i++)for(j=1; j<=n; j++)cin>>mat[i][j];
}
void showmat()
{
int i,j;
for(i=1; i<=n; i++)
{
cout<<endl;
for(j=1; j<=n; j++)cout<<mat[i][j]<<" ";
}
}
};

template <class T>
class C2: public C1<T>
{
C2(int a): C1(a) {};

};

每当我运行它时,我都会收到错误:

In constructor C2::C2(int)':

error: class 'C2' does not have a field named 'C1'

如果有人能向我解释我做错了什么,我将不胜感激。

最佳答案

你应该将模板参数添加到基类

template <class T>
class C2: public C1<T>
{
C2(int a): C1<T>(a) {};

};

关于C++模板类继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37499791/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com