【问题标题】:Inheritance and templates in C++C++ 中的继承和模板
【发布时间】:2013-10-01 09:29:54
【问题描述】:

在我的研究中,我被要求将结构 Queue 实现为 2-way Linked List,其方法允许从两端删除元素。 我还被要求制作从队列继承的 sracture 堆栈(例如堆栈是派生类),支持 push 和 pop 方法,以便 push&pop 将使用父方法从一端插入\取出。

我还创建了一个节点类来生成链表。另外,我不允许更改“主要”。 我被要求实现它两次(两个单独的程序,两个单独的主要功能):一个用于双精度类型,一个用于泛型类型(模板)。 “双”程序工作正常,但通用程序遇到很多错误,我真的不知道这意味着什么,与全局命名空间有关。另外,我检查了缺少的语法,如分号/军营等,似乎没问题。

我在帖子末尾附上了程序和通用程序的错误:

“双重”程序(工作正常):

// ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>


using namespace std;

class dNode
{
private:
         dNode* prev_value;
         dNode* next_value;
         double data_value;
         friend class ddutor;
         friend class dstack;
public:
    //default constructor
    dNode(double val);
    ~dNode();
};

 dNode::dNode(double val)
 {
     next_value=NULL;
     prev_value=NULL;
     data_value=val;
 }

 dNode::~dNode()
 {
    data_value=-1;
 }


/*************************************************************************************************************************************/


 class ddutor
{
protected:
        dNode* current;
        dNode* head;
        dNode* tail;
        int size;
public:
    //default constructor
    ddutor(double val);
    //destructor
    ~ddutor();
    //members functions
    void add_first(double data);
    void add_last(double data);
    double remove_first();
    void remove_last();

};


ddutor::ddutor(double val)
{
            head=new dNode(val);
            tail = new dNode(-1);
            tail=head;
            head->next_value=tail;
            head->prev_value=NULL;
            size=1;
}

void ddutor::add_first(double data)
        {
            current = new dNode(data);
            head->prev_value = current;
            current->next_value = head;
            head = current;
            head->prev_value= NULL;
            size++;
        }
        void ddutor::add_last(double data)
        {
            current = new dNode(data);
            tail->next_value = current;
            current->prev_value= tail;
            tail = current;
            tail->next_value = NULL;
            size++;
        }
        double ddutor::remove_first()
        {

            double temp=head->data_value;
            if (head->next_value == NULL)
                head = NULL;
            else
                head = head->next_value;
            size--;
            return temp;
        }
        void ddutor::remove_last()
        {

            if (tail->prev_value == NULL)
                tail = NULL;
            else
                tail = tail->prev_value;
            size--;

        }
        ddutor::~ddutor()
        {
            delete current;
            delete head;
            delete tail;
            size=-1;
        }


/******************************************************************************************************************************************/

        class dstack:public ddutor
        {

        public:
            void push(double d);
            double pop();
            dstack(double d);

        };

        dstack::dstack(double d):ddutor(d){};
        void dstack::push(double d)
        {
            add_last(d);
        }
        double dstack::pop()
        {
            double temp=tail->data_value;
            remove_last();
            return temp;
        }


/*******************************************************************************************************************************/


int main()
{
  int i;
  ddutor dd(1.1);
  dstack ds(11.1);
  for(i=2; i < 14; i += 2)
  {
    dd.add_first((double)i*1.1); 
    dd.add_last((double)((i+1)*1.1)); 
  } //for
  cout<<"dd print:\n";
  for(i=1; i < 14; i++)
    cout << "  " << dd.remove_first(); 
  cout << endl;

  for(i=2; i < 14; i ++)
    ds.push((double)i*11.1); 
  cout << "ds print:\n";
  for(i=1; i < 14; i++)
    cout << "  " << ds.pop(); 
  cout << endl;
cin>>i;
} // main

“通用程序(很多错误)

#include "stdafx.h"
#include <iostream>


using namespace std;

template <class ELEMENT_TYPE>
class gNode
{
private:
    gNode* prev_value;
    gNode* next_value;
    ELEMENT_TYPE data_value;
    friend class tdutor;
    friend class tstack;
public:
    //default constructor
    gNode(ELEMENT_TYPE val);
    ~gNode();
};

template <class ELEMENT_TYPE> 
gNode<ELEMENT_TYPE>::gNode(ELEMENT_TYPE val)
 {
     next_value=NULL;
     prev_value=NULL;
     data_value=val;
 }

template <class ELEMENT_TYPE>  
gNode<ELEMENT_TYPE>::~gNode()
 {
    data_value=-1;
 }

     //***************************************************************************************************************************//

template <class ELEMENT_TYPE>     
class tdutor
     {
     protected:
         gNode* current;
         gNode* head;
         gNode* tail;
         int size;
    public:
    //default constructor
    tdutor(ELEMENT_TYPE val);
    //destructor
    ~tdutor();
    //members functions
    void add_first(ELEMENT_TYPE data);
    void add_last(ELEMENT_TYPE data);
    ELEMENT_TYPE remove_first();
    void remove_last();

};


template <class ELEMENT_TYPE>  
   tdutor<ELEMENT_TYPE>::tdutor(ELEMENT_TYPE val)
{
            head=new gNode(val);
            tail = new gNode(-1);
            tail=head;
            head->next_value=tail;
            head->prev_value=NULL;
            size=1;
}
template <class ELEMENT_TYPE> 
void  tdutor<ELEMENT_TYPE>::add_first(ELEMENT_TYPE data)
        {
            current = new gNode(data);
            head->prev_value = current;
            current->next_value = head;
            head = current;
            head->prev_value= NULL;
            size++;
        }
template <class ELEMENT_TYPE> 
        void  tdutor<ELEMENT_TYPE>::add_last(ELEMENT_TYPE data)
        {
            current = new gNode(data);
            tail->next_value = current;
            current->prev_value= tail;
            tail = current;
            tail->next_value = NULL;
            size++;
        }

template <class ELEMENT_TYPE> 
        ELEMENT_TYPE tdutor<ELEMENT_TYPE>::remove_first()
        {

            ELEMENT_TYPE temp=head->data_value;
            if (head->next_value == NULL)
                head = NULL;
            else
                head = head->next_value;
            size--;
            return temp;
        }

template <class ELEMENT_TYPE> 
        void tdutor<ELEMENT_TYPE>::remove_last()
        {

            if (tail->prev_value == NULL)
                tail = NULL;
            else
                tail = tail->prev_value;
            size--;

        }
template <class ELEMENT_TYPE> 
        tdutor<ELEMENT_TYPE>::~tdutor()
        {
            delete current;
            delete head;
            delete tail;
            size=-1;
        }

/*******************************************************************************************************************************/

template <class ELEMENT_TYPE>
class tstack:public tdutor
{   
        public:
            void push(ELEMENT_TYPE d);
            ELEMENT_TYPE pop();
            tstack(ELEMENT_TYPE d);

        };
template <class ELEMENT_TYPE>
        tstack<ELEMENT_TYPE>::tstack(ELEMENT_TYPE d):tdutor(d){};
template <class ELEMENT_TYPE>
        void tstack<ELEMENT_TYPE>::push(ELEMENT_TYPE d)
        {
            add_last(d);
        }
template <class ELEMENT_TYPE>
        ELEMENT_TYPE tstack<ELEMENT_TYPE>::pop()
        {
            ELEMENT_TYPE temp=tail->data_value;
            remove_last();
            return temp;
        }



int main()
{
  int i;
  tdutor<double> dd(1.1);
  tstack<double> ds(11.1);


  for(i=2; i < 14; i += 2)
  {
    dd.add_first((double)i*1.1); 
    dd.add_last((double)((i+1)*1.1)); 
  } //for

  cout << "dd print:\n";
  for(i=1; i < 14; i++)
    cout << "  " << dd.remove_first(); 
  cout << endl;

  for(i=2; i < 14; i ++)
    ds.push((double)i*11.1); 

  cout << "ds print:\n";
  for(i=1; i < 14; i++)
    cout << "  " << ds.pop(); 
  cout << endl;
  cin>>i;
} // main

泛型错误:

错误 23 错误 C1903:无法从先前的错误中恢复;停止编译

错误 5 错误 C2039: 'add_first' : is not a member of '`global namespace''

错误 12 错误 C2039: 'add_last' : is not a member of '`global namespace''

错误 13 错误 C2039: 'remove_first' : is not a member of '`global namespace''

错误 21 错误 C2039: 'remove_last' : is not a member of '`global namespace''

错误 4 错误 C2059:语法错误:'

错误 11 错误 C2059:语法错误:'

错误 20 错误 C2059:语法错误:'

错误 18 错误 C2086:'int tdutor':重新定义

错误 6 错误 C2143:语法错误:缺少 ';'在'{'之前

错误 14 错误 C2143:语法错误:缺少 ';'在'{'之前

错误 8 错误 C2143:语法错误:缺少 ';'在'

错误 16 错误 C2143:语法错误:缺少 ';'在'

错误 9 错误 C2182: 'tdutor' : 非法使用类型 'void'

错误 17 错误 C2182: 'tdutor' : 非法使用类型 'void'

错误 7 错误 C2447: '{' : 缺少函数头(旧式正式列表?)

错误 15 错误 C2447: '{' : 缺少函数头(旧式正式列表?)

错误 22 错误 C2588: '::~tdutor' : 非法的全局析构函数

错误3错误C2988:无法识别的模板声明/定义

错误 10 错误 C2988:无法识别的模板声明/定义

错误 19 错误 C2988:无法识别的模板声明/定义

错误 1 ​​错误 C2989: 'tdutor' : 类模板已被声明为非类模板

错误 2 错误 C3857: 'tdutor': 不允许多个模板参数列表

【问题讨论】:

  • 您应该指定“很多错误”,以便每个人都知道问题出在哪里。尤其是第一个编译器错误最有帮助。
  • Visual Studio 中的“错误”窗口包含不完整的信息,尤其是在涉及模板时。请从输出窗口复制错误,包括它们周围的任何“注释”。这些列出了编译器试图生成的特化,没有它们,错误可能没有意义。
  • 除了您粘贴的列表之外,所有内容都混淆了。按照编译器生成错误的顺序复制错误。

标签: c++ templates generics inheritance


【解决方案1】:

你说

friend class tdutor;
friend class tstack;

在 gNode 的中间,它从那里下山。 VS 似乎认为这定义了一个非模板 tdutor 和 tstack。

没有这些行你只会得到ione错误:

error C2955: 'tdutor' : use of class template requires template argument list   

class tstack:public tdutor

改成

class tstack:public tdutor<ELEMENT_TYPE>

然后将错误转移到有关 gNode 需要模板参数列表的投诉。

一旦你把这些都追完了,你就需要考虑一下朋友声明了。

一些线索:here

【讨论】:

    【解决方案2】:

    你没有指定tdutor的模板参数。

    您应该将代码更改为:

    template <class ELEMENT_TYPE>
    class tstack : public tdutor<ELEMENT_TYPE>
    

    【讨论】:

      【解决方案3】:

      每当您实例化您的模板类之一时,您也必须提供模板参数。

      所以不要写

      gNode* tail;
      

      你必须写

      gNode<ELEMENT_TYPE>* tail;
      

      有很多这样的问题(包括你必须给出类型的继承)。

      找出所有这些会对你有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多