从模板模板类继承的类模板

时间:2014-10-13 18:33:20

标签: c++ templates inheritance

我将模板类Term放入另一个名为List的类的参数列表中,因此类List将是模板模板类。

然后我还写了一个名为Polynomial的类,它继承自List类。

包含类Polynomial必须访问类Term的私有成员,因此我使类Polynomial成为类Term和类List的朋友。

我的代码的第一部分是" prog.h" ↓

//template <class S> class Term;
//template<template<typename> class Term >class List;

template <class T> class Polynomial: public List < Term<typename>,T > {
public:
    Polynomial<T>():List(){}

    void Add(const Polynomial<T>& b)
    {
        int i=0;
        Polynomial<T> c;
        int a_pos=0,b_pos=0;
        c.term_num=0;
        while((a_pos < term_num) && (b_pos < b.term_num))
        {
            if(terms[a_pos].expo == b.terms[b_pos].expo)
            {
                T tt=terms[a_pos].coef + b.terms[b_pos].coef;
                if(tt){c.newItem(Term<T>(terms[a_pos].expo,tt));}
                a_pos++;b_pos++;
            }
            else if(terms[a_pos].expo < b.terms[b_pos].expo)
            {
                c.newItem(Term<T>(b.terms[b_pos].expo,b.terms[b_pos].coef));
                //c.term_num++;
                b_pos++;
            }
            else
            {
                c.newItem(Term<T>(terms[a_pos].expo,terms[a_pos].coef));
                //c.term_num++;
                a_pos++;
            }
        }

        for(;a_pos<term_num;a_pos++){
            c.newItem(Term<T>(terms[a_pos].expo,terms[a_pos].coef));
            //c.term_num++;
        }

        for(;b_pos<b.term_num;b_pos++){
            c.newItem(Term<T>(b.terms[b_pos].expo,b.terms[b_pos].coef));
            //c.term_num++;
        }

        int pos_;
        for(pos_=0;pos_<term_num;pos_++)
        {
            terms[pos_].expo=c.terms[pos_].expo;
            terms[pos_].coef=c.terms[pos_].coef;
            //cout<<c.terms[pos_].expo<<endl;
            //cout<<c.terms[pos_].coef<<endl;
        }
        for(;pos_<c.term_num;pos_++)
        {
            newItem(Term<T>(c.terms[pos_].expo,c.terms[pos_].coef));
            //cout<<c.terms[pos_].expo<<endl;
            //cout<<c.terms[pos_].coef<<endl;
        }

    }
    void Print()
    {
        int pos;
        for(pos=0;pos<term_num;pos++)
            cout<<"<"<<terms[pos].expo<<","<<terms[pos].coef<<">";
        cout<<endl;
    }
};

template <class S> class Term
{
friend class Polynomial<S>;
private:
    S coef;
    int expo;
public:
    Term<S>(){expo=0;coef=0;}
    Term<S>(int ex,S co)
    {
        expo=ex;
        coef=co;
    }
};

template < template <typename> class Term ,typename T >
class List{
private:
    int capacity;
    int term_num;
    Term<T>* terms;
public:
    friend class Polynomial<T>;
    List()
    {
        capacity=5;
        term_num=0;
        terms = new Term<T>[capacity];
    }

    void newItem(const Term<T>& t)
    {
        terms[term_num].expo=t.expo;
        terms[term_num].coef=t.coef;
        term_num++;

        if(term_num==capacity)
        {
            capacity*=2;
            Term<T>* temp=new Term<T>[capacity];
            copy(terms, terms + term_num, temp);
            delete[]terms;
            terms=temp;
        }
    }
};

我的代码的第二部分是&#34;主要功能&#34; ↓

#include <cstdlib>
#include <iostream>

using namespace std;
#include "prog.h"    // change to your header file here

    int main(int argc, char *argv[])
    {
      Polynomial<double> poly1, poly2;
      // polynomial #1
      poly1.newItem(Term<double>(5,15));
      poly1.newItem(Term<double>(3,-6));
      poly1.newItem(Term<double>(2,-14));
      poly1.newItem(Term<double>(0,13.2));
      cout << "Polynomial #1 = ";
      poly1.Print();
      // polynomial #2
      poly2.newItem(Term<double>(6,10));
      poly2.newItem(Term<double>(3,6));
      poly2.newItem(Term<double>(2,4));
      poly2.newItem(Term<double>(1,-2));
      cout << "Polynomial #2 = ";
      poly2.Print();
      // addition
      poly1.Add(poly2);
      cout << "addition : ";
      poly1.Print();
      system("PAUSE");
      return 0;
    }

当我尝试编译时,编译器会显示一些错误:

(prog.h)
Line 5    error:expected template-name before '<' token
Line 5    error:expected `{' before '<' token
Line 5    error:expected unqualified-id before '<' token
Line 5    error:expected `;' before '<' token
(main)
Line 9    aggregate`Polynomial<double> poly1' has incomplete type and can not be defined
Line 9    aggregate`Polynomial<double> poly2' has incomplete type and can not be defined

我想知道我的代码有什么不对,因为我真的不明白编译器的含义......

0 个答案:

没有答案