-2

I'm asked in my study to make structure Queue implemented as 2-way Linked List with methods that allow to remove elements from both ends. I'm also asked to make sracture stack that inheritage from Queue (e.g stack is derived class) that support method of push and pop such that push&pop will use the father method to insert\take out from one of the ends.

I also created a node class for generate the linked list. also, I'm not allowed to change the "main". I'm asked to implement it twice (two seperate programs, two seperate main functions): one for double type and one generic type (tamplate). the "double" program works fine, but the generic program encountering a lot of errors that I dont really no what it means, something with global namespace. also, i've checked for missing syntax like semicolon/ barracks and such and it seems to be fine.

I attach both of the programs and the errors of the generic program at the end of the post:

the "double" program (works fine):

// 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

the "generic program (lot of errors)

#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

errors of the generic:

Error 23 error C1903: unable to recover from previous error(s); stopping compilation

Error 5 error C2039: 'add_first' : is not a member of '`global namespace''

Error 12 error C2039: 'add_last' : is not a member of '`global namespace''

Error 13 error C2039: 'remove_first' : is not a member of '`global namespace''

Error 21 error C2039: 'remove_last' : is not a member of '`global namespace''

Error 4 error C2059: syntax error : '<'

Error 11 error C2059: syntax error : '<'

Error 20 error C2059: syntax error : '<'

Error 18 error C2086: 'int tdutor' : redefinition

Error 6 error C2143: syntax error : missing ';' before '{'

Error 14 error C2143: syntax error : missing ';' before '{'

Error 8 error C2143: syntax error : missing ';' before '<'

Error 16 error C2143: syntax error : missing ';' before '<'

Error 9 error C2182: 'tdutor' : illegal use of type 'void'

Error 17 error C2182: 'tdutor' : illegal use of type 'void'

Error 7 error C2447: '{' : missing function header (old-style formal list?)

Error 15 error C2447: '{' : missing function header (old-style formal list?)

Error 22 error C2588: '::~tdutor' : illegal global destructor

Error 3 error C2988: unrecognizable template declaration/definition

Error 10 error C2988: unrecognizable template declaration/definition

Error 19 error C2988: unrecognizable template declaration/definition

Error 1 error C2989: 'tdutor' : class template has already been declared as a non-class template

Error 2 error C3857: 'tdutor': multiple template parameter lists are not allowed

Ozair Kafray
  • 13,351
  • 8
  • 59
  • 84
Shay
  • 45
  • 6
  • You should specify 'lot of errors' so that everyone knows where the problem is. Especially the first compiler errors are most helpful. – Till Oct 01 '13 at 09:33
  • 1
    The "errors" window in Visual Studio contains incomplete information, especially when templates are involved. Please, copy the errors from the output window including any "notes" around them. Those list the specializations the compiler is trying to generate and without them the errors may not make sense. – Jan Hudec Oct 01 '13 at 09:42
  • 1
    Besides the list you pasted is all mixed up. Copy the errors in the order the compiler generated them. – Jan Hudec Oct 01 '13 at 09:43

3 Answers3

2

You say

friend class tdutor;
friend class tstack;

in the middle of gNode and it goes down-hill from there. VS seems to think this defines a non-template tdutor and tstack.

Without these lines you just get ione error:

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

on class tstack:public tdutor

Changing this to

class tstack:public tdutor<ELEMENT_TYPE>

then moves the errors to complaints about gNode requiring a template argument list.

Once you have chased all these down, you need to think about the friend declaration.

Some clues: here

Community
  • 1
  • 1
doctorlove
  • 18,872
  • 2
  • 46
  • 62
1

You didn't specify the template parameter of tdutor.

You should change your code to:

template <class ELEMENT_TYPE>
class tstack : public tdutor<ELEMENT_TYPE>
Asaf
  • 4,317
  • 28
  • 48
1

Whenever you instantiate one of your template classes you will have to give the template parameter as well.

So instead of writing

gNode* tail;

you will have to write

gNode<ELEMENT_TYPE>* tail;

There are a lot of these issues (including inheritance where you have to give the type as well).

Hunting all of these down will help you.

Till
  • 682
  • 1
  • 6
  • 19