标题:最后一句话不知道哪里错了?
只看楼主
hjzhappy
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2014-3-4
 问题点数:0 回复次数:1 
最后一句话不知道哪里错了?
#include<iostream>
#include<string>
using namespace std;
const int MaxSize=100;


template <class DataType>


struct Node
{
      int coef;
      int exp;

      Node<DataType> *next;
};


template <class DataType>


class LinkList
{
public:
    LinkList( );                            //无参构造函数,建立只有头结点的空链表
    LinkList(DataType a[ ], int n);            //有参构造函数,建立有n个元素的单链表
    ~LinkList( );                            //析构函数
    void PrintList( );                        //遍历操作,按序号依次输出各元素
    friend void Add(LinkList<int> *A,LinkList<int> *B);
private:
    Node<DataType> *first;                    //单链表的头指针
};

template <class DataType>
LinkList<DataType> :: LinkList( )
{
    first = new Node<DataType>;              //生成头结点
    first->next = NULL;                      //头结点的指针域置空
}

template <class DataType>  
LinkList<DataType> :: LinkList(DataType a[ ], int n)
{
    Node<DataType> *s;
    first = new Node<DataType>;                    //生成头结点
    first->next=NULL;                            //尾指针初始化
    for (int i = 0; i < n; i+=2)
    {
        s = new Node<DataType>;
        s->coef=a[i];                      //为每个数组元素建立一个结点
        s->exp=a[i+1];
   
        s->next = first->next;
        first->next=s;
                                    //将结点s插入到终端结点之后
    }
                                    //单链表建立完毕,将终端结点的指针域置空
}

template <class DataType>
LinkList<DataType> :: ~LinkList( )
{
    Node<DataType> *q;
    while (first != NULL)                //释放单链表的每一个结点的存储空间
    {
        q = first;                        //暂存被释放结点
        first = first->next;            // first指向被释放结点的下一个结点
        delete q;   
    }
}

template <class DataType>
void LinkList<DataType> :: PrintList( )
{   
    Node<DataType> *p = first->next;                 //工作指针p初始化
    while (p!= NULL)
    {  
        cout << p->coef;
        cout<<"*x^";
        cout<<p->exp;
        if(p->next!= NULL) cout<<"+";
        p = p->next;                                //工作指针p后移,注意不能写作p++
    }
    cout<<endl;
}

template <class DataType>
void Add(LinkList<int> *A,LinkList<int> *B)
{
    Node*pre,*p,*qre,*q;
pre=A.first;p=pre->next;                           //工作指针pre和p初始化
qre=B.first;q=qre->next;                           //工作指针qre和q初始化
while(P!=NULL&&q!=NULL)
{
    if(p->exp < q->exp){                              //第1种情况
        pre=p;
        p=p->next;
    }
    else if(p->exp > q->exp){
        Node *v;                                  //第二种情况
        v=q->next;
        pre->next=q;                                //将结点q插入到结点p之前
        q->next=p;
        q=v;
    }


    else{                                            //第三种情况
        p->coef=p->coef+q->coef;                     //系数相加
        if(p->coef==0){                              //系数为0,删除结点p和结点q
            pre->next=p->next;                       //删除结点q
            delete p;
            p=pre->next;
        }

        else{                                        //系数不为0,只删除结点q
            pre=p;
            p=p->next;
        }
    qre->next=q->next;                             //删除结点q
    delete q;
    q=qre->next;
    }

}

if(q!=NULL)pre->next=q;                            //将结点q链接在第一个单链表的后面
delete B.first;

 
//return A;                                  //释放第二个单链表的头结点所占的内存
}

void main(){
   int i,len,a[MaxSize];
   
   string str;
   cout<<"请连续输入一元多项式的系数与指数:";
   cin>>str;
   len=str.length();
   for(i=0; i<len; i++) {
        if(str.at(i)>='0'&&str.at(i)<='9') {
            a[i]= str.at(i)-'0';
            
        }

}


   //for(int k=0;k<len;k++)
        //cout<<a[k];
   cout<<"一元多项式A:";
LinkList<int> A(a,len);

A.PrintList();


  int j,len1,b[MaxSize];
  
   string str1;
   cout<<"请连续输入一元多项式的系数与指数:";
   cin>>str1;
   len1=str1.length();
   for(j=0; j<len1; j++) {
        if(str.at(j)>='0'&&str1.at(j)<='9') {
            b[j]= str1.at(j)-'0';
            
        }

}
   cout<<"一元多项式B:";
LinkList<int> B(b,len1);

B.PrintList();


cout<<"结果是:";
Add(A,B).PrintList(); // 这句有错、、、、、

}
error C2783: 'void __cdecl Add(class LinkList<int> *,class LinkList<int> *)' : could not deduce template argument for 'DataType'
 error C2228: left of '.PrintList' must have class/struct/union type#include<iostream>
#include<string>
using namespace std;
const int MaxSize=100;


template <class DataType>


struct Node
{
      int coef;
      int exp;

      Node<DataType> *next;
};


template <class DataType>


class LinkList
{
public:
    LinkList( );                            //无参构造函数,建立只有头结点的空链表
    LinkList(DataType a[ ], int n);            //有参构造函数,建立有n个元素的单链表
    ~LinkList( );                            //析构函数
    void PrintList( );                        //遍历操作,按序号依次输出各元素
    friend void Add(LinkList<int> *A,LinkList<int> *B);
private:
    Node<DataType> *first;                    //单链表的头指针
};

template <class DataType>
LinkList<DataType> :: LinkList( )
{
    first = new Node<DataType>;              //生成头结点
    first->next = NULL;                      //头结点的指针域置空
}

template <class DataType>  
LinkList<DataType> :: LinkList(DataType a[ ], int n)
{
    Node<DataType> *s;
    first = new Node<DataType>;                    //生成头结点
    first->next=NULL;                            //尾指针初始化
    for (int i = 0; i < n; i+=2)
    {
        s = new Node<DataType>;
        s->coef=a[i];                      //为每个数组元素建立一个结点
        s->exp=a[i+1];
   
        s->next = first->next;
        first->next=s;
                                    //将结点s插入到终端结点之后
    }
                                    //单链表建立完毕,将终端结点的指针域置空
}

template <class DataType>
LinkList<DataType> :: ~LinkList( )
{
    Node<DataType> *q;
    while (first != NULL)                //释放单链表的每一个结点的存储空间
    {
        q = first;                        //暂存被释放结点
        first = first->next;            // first指向被释放结点的下一个结点
        delete q;   
    }
}

template <class DataType>
void LinkList<DataType> :: PrintList( )
{   
    Node<DataType> *p = first->next;                 //工作指针p初始化
    while (p!= NULL)
    {  
        cout << p->coef;
        cout<<"*x^";
        cout<<p->exp;
        if(p->next!= NULL) cout<<"+";
        p = p->next;                                //工作指针p后移,注意不能写作p++
    }
    cout<<endl;
}

template <class DataType>
void Add(LinkList<int> *A,LinkList<int> *B)
{
    Node*pre,*p,*qre,*q;
pre=A.first;p=pre->next;                           //工作指针pre和p初始化
qre=B.first;q=qre->next;                           //工作指针qre和q初始化
while(P!=NULL&&q!=NULL)
{
    if(p->exp < q->exp){                              //第1种情况
        pre=p;
        p=p->next;
    }
    else if(p->exp > q->exp){
        Node *v;                                  //第二种情况
        v=q->next;
        pre->next=q;                                //将结点q插入到结点p之前
        q->next=p;
        q=v;
    }


    else{                                            //第三种情况
        p->coef=p->coef+q->coef;                     //系数相加
        if(p->coef==0){                              //系数为0,删除结点p和结点q
            pre->next=p->next;                       //删除结点q
            delete p;
            p=pre->next;
        }

        else{                                        //系数不为0,只删除结点q
            pre=p;
            p=p->next;
        }
    qre->next=q->next;                             //删除结点q
    delete q;
    q=qre->next;
    }

}

if(q!=NULL)pre->next=q;                            //将结点q链接在第一个单链表的后面
delete B.first;

 
//return A;                                  //释放第二个单链表的头结点所占的内存
}

void main(){
   int i,len,a[MaxSize];
   
   string str;
   cout<<"请连续输入一元多项式的系数与指数:";
   cin>>str;
   len=str.length();
   for(i=0; i<len; i++) {
        if(str.at(i)>='0'&&str.at(i)<='9') {
            a[i]= str.at(i)-'0';
            
        }

}


   //for(int k=0;k<len;k++)
        //cout<<a[k];
   cout<<"一元多项式A:";
LinkList<int> A(a,len);

A.PrintList();


  int j,len1,b[MaxSize];
  
   string str1;
   cout<<"请连续输入一元多项式的系数与指数:";
   cin>>str1;
   len1=str1.length();
   for(j=0; j<len1; j++) {
        if(str.at(j)>='0'&&str1.at(j)<='9') {
            b[j]= str1.at(j)-'0';
            
        }

}
   cout<<"一元多项式B:";
LinkList<int> B(b,len1);

B.PrintList();


cout<<"结果是:";
Add(A,B).PrintList(); // 这句有错、、、、、

}
error C2783: 'void __cdecl Add(class LinkList<int> *,class LinkList<int> *)' : could not deduce template argument for 'DataType'
 error C2228: left of '.PrintList' must have class/struct/union type
搜索更多相关主题的帖子: include public 元素 
2014-03-04 21:09
hjzhappy
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2014-3-4
得分:0 
额、、、发了两遍。。。不好意思、、、
2014-03-04 21:12



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-428014-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.075440 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved