6 Replies - 4510 Views - Last Post: 20 March 2013 - 04:03 PM Rate Topic: -----

#1 codeNtheory  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 14-February 13

2 errors with template, Expected un-qualified-id & Unknown type na

Posted 18 March 2013 - 09:54 PM

Hi everybody I am new to template and can not figure out these errors. I am using xcode for an ID, which could be part of the problem. My teacher told me to switch the list.tem to list_tem.h because it is easier for an ide to read. It did not fix the problem, here is what i have so far. Both errors are in the list_tem.h file, displayed on the copy constructor. Any help would be appreciated, thank you.

list.h file:
#ifndef __FwrdLinkList__list__
#define __FwrdLinkList__list__

#include <iostream>

// To be developed once the lecture is completed
//#include "node_iterator.h"

// Class definition
template <typename T>
class list
{

public:
    //typedef node_iterator<T> iterator;
    
    // Constructors **********************************************************
    
    //Pre:  Default constructor
    //Post: Constructor just sets the head pointer to NULL
    list():head(0), used(0){}
    
    //Pre:
    //Post:
    list(const list<T>& copy);
 ~list();
    //Pre:
    //Post:
    void pushFront(const T&);
private:
    
    // node struct needs to be at top for template!
    struct node
    {
        T data;
        node* link;
        node(const T& val=T(), node* n=0):data(val),link(n) {}
    };
    node* head;
    unsigned used;
    node* copy(node*);
};

#include "list_tem.h"

#endif /* defined(__FwrdLinkList__list__) */



list_tem.h file
template <typename T>
list<T>::list(const list& copy)          //HERE IS BOTH ERRORS ( Expected unqualified-id,  unknown type name 'list' )
{
    typename list<T>::node* temp = new typename list<T>::node(*copy.head);
    this->head = temp;
    this->used = copy.used;
}

template <typename T>
list<T>::~list()
{
    node *tmp;
    while (head)
    {
        tmp = head;
        head = head->link;
        delete tmp;
    }
}

template <typename T>
void list<T>::pushFront(const T& val)
{
    node *newNode = new node(val,0);
    used++;
    if (used == 0)
    {
        head = newNode;
        
        return;
    }
    newNode->link = head;
    head = newNode;
    
    return;
}



Is This A Good Question/Topic? 0
  • +

Replies To: 2 errors with template, Expected un-qualified-id & Unknown type na

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1396
  • View blog
  • Posts: 4,872
  • Joined: 19-February 09

Re: 2 errors with template, Expected un-qualified-id & Unknown type na

Posted 18 March 2013 - 10:13 PM

In the class the copy constructor is defined correctly :

25	    list(const list<T>& copy);




In list_tem.h

01	template <typename T>
02	list<T>::list(const list& copy)    




Edit: it was not that line but

typename list<T>::node* temp = new typename list<T>::node(*copy.head);


which should not have the typename keywords.

list<T>::node* temp = new list<T>::node(*copy.head);


Hopefully that is right now!

.

This post has been edited by #define: 18 March 2013 - 10:24 PM

Was This Post Helpful? 0
  • +
  • -

#3 codeNtheory  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 14-February 13

Re: 2 errors with template, Expected un-qualified-id & Unknown type na

Posted 19 March 2013 - 07:07 AM

View Post#define, on 18 March 2013 - 10:13 PM, said:

In the class the copy constructor is defined correctly :

25	    list(const list<T>& copy);




In list_tem.h

01	template <typename T>
02	list<T>::list(const list& copy)    




Edit: it was not that line but

typename list<T>::node* temp = new typename list<T>::node(*copy.head);


which should not have the typename keywords.

list<T>::node* temp = new list<T>::node(*copy.head);


Hopefully that is right now!

.


That did not fix the problem. I think that typename is supposed to be there because I am creating a struct node inside the class. Now if it were in its own class this probably would be a little easier, but I can't do that. I just tried it in my text editor, sublime, as well with no luck. Thank You again
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4232
  • View blog
  • Posts: 13,281
  • Joined: 25-December 09

Re: 2 errors with template, Expected un-qualified-id & Unknown type na

Posted 19 March 2013 - 07:15 AM

First just saying it doesn't work doesn't tell us anything. Why didn't it fix the problem?

If you are getting error messages, post the complete error messages, exactly as they appear in your development environment. Also because these are templates you need to post a sample of how you are trying to use these classes.

Jim
Was This Post Helpful? 1
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1396
  • View blog
  • Posts: 4,872
  • Joined: 19-February 09

Re: 2 errors with template, Expected un-qualified-id & Unknown type na

Posted 19 March 2013 - 06:21 PM

I get an access violation in the function, because head in the list to copy is null, in an empty list.

You are correct that typename does not affect the statement, they are not needed. The node structure is a member of the list class so the list<T>:: scope is not required either.

The node constructor's first parameter is of type T
  node(const T& val = T(), node* n=0): data(val),link(n) {}



and you are trying to pass a node, whereas you want data.

template <typename T>
list<T>::list(const list<T>& list_copy)
{
  node* temp = new node( list_copy.head->data );
}



To implement the list copy constructor you will want to delete any data in the current list, and copy the data from the other list. Delete and copy are useful functions to make because they can be used elsewhere, and also make the class easier to understand.

Useful functions
1) is empty
2) print
3) delete all nodes
4) copy all
etc

You are using the used variable, it is common to use head set to NULL or 0 for an empty list.
Was This Post Helpful? 1
  • +
  • -

#6 codeNtheory  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 14-February 13

Re: 2 errors with template, Expected un-qualified-id & Unknown type na

Posted 20 March 2013 - 03:14 PM

View Postjimblumberg, on 19 March 2013 - 07:15 AM, said:

First just saying it doesn't work doesn't tell us anything. Why didn't it fix the problem?

If you are getting error messages, post the complete error messages, exactly as they appear in your development environment. Also because these are templates you need to post a sample of how you are trying to use these classes.

Jim


Sorry for being so abstract, I did fix the problem though. Typename did not need to be on the new node instantiation, so you were on the right track #define. Here is my working result of the copy constructor.

list_tem.h file
template <typename T>
list<T>::list(const list<T>& copy)
{
    typename list<T>::node* temp = new node(*copy.head);
    this->head = temp;
    this->used = copy.used;
}



Thanks again.
Was This Post Helpful? 0
  • +
  • -

#7 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1396
  • View blog
  • Posts: 4,872
  • Joined: 19-February 09

Re: 2 errors with template, Expected un-qualified-id & Unknown type na

Posted 20 March 2013 - 04:03 PM

It doesn't look as if the list will be copied. You just point to another object's list.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1