簡體   English   中英

C ++模板類繼承

[英]C++ template class inheritance

#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) {};

};

每當我運行它時,都會出現錯誤:

在構造函數C2 :: C2(int)'中:

錯誤:類別“ C2”沒有名為“ C1”的字段

如果有人可以向我解釋我做錯了什么,我將不勝感激。

您應該將template參數添加到基類

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

};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM