Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-15-2008, 02:43 PM
hellochar hellochar is offline
Newbie
 
Join Date: Jul 2008
Posts: 10
Rep Power: 0
hellochar is on a distinguished road
Default class prototype and template error

I'm writing a linked list program, and I'm using templates to specify which type of data the linked list will hold. In order to let other programs use my linked list, I need a header for the classes and functions. I must be declaring the headers wrong or implementing them wrong, because my compiler is giving me a crapload of errors.

My header linkedlist.h looks something like this

Code:
#ifndef LINKEDLIST_H
#define LINKEDLIST_H

template <class E>
class Link;

template <class E>
class LinkedList;

template <class E> //retrieves first link
Link<E> LinkedList::first(void); //Gives me error: " 'template<class E> struct LinkedList' used without template parameters

template <class E> //add link to end of list
void LinkedList::add(Link<E> *l); //same error

...
...

#endif /* LINKEDLIST_H */
Here is my linkedlist.cpp

Code:
#include "linkedlist.h"

template <class E>
class Link {
    public:
    E *data;
    Link<E> *next;

    Link(void) {
    }

    Link(E *data) {
        this.data = data;
    }
};

template <class E>
class LinkedList {

    public:
    const Link<E> *const head ();
    Link<E> *const last;

    LinkedList(void) {
        last = head;
    }

    LinkedList(Link<E> l) {
        this();
        add(l);
    }

    LinkedList(E o) {
        this(new Link<E>(o));
    }

    Link<E>* first(void) {
        return head->next;
    }

    void add(Link<E> *l) {
        last->next = l;
        last = l;
    }

...
...

}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 07-15-2008, 02:47 PM
dcs dcs is offline
Programming Expert
 
Join Date: Mar 2008
Posts: 371
Rep Power: 6
dcs has a spectacular aura aboutdcs has a spectacular aura about
Default Re: class prototype and template error

At that point Link and LinkedList are not yet defined. In order to return a value, of that type, it needs to know the definition (to know how big it is, among other things). I don't suppose you were trying to return pointers instead?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-15-2008, 04:12 PM
hellochar hellochar is offline
Newbie
 
Join Date: Jul 2008
Posts: 10
Rep Power: 0
hellochar is on a distinguished road
Default Re: class prototype and template error

Originally I was trying to return pointers, but the compiler gave me another error. For instance, when I had

Code:
*Link<E> LinkedList::first(void);
There was an error stating "expected constructor, destructor, or type conversion before "LinkedList"" Although that did remove the other error. But that doesn't really help to replace one error with another hehe

editops, turns out i wrote that wrong. Should have been
Code:
 Link<E> *LinkedList::first(void);
but when I do that, the original error comes back. so still no dice

Last edited by hellochar; 07-15-2008 at 05:05 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-15-2008, 04:18 PM
dcs dcs is offline
Programming Expert
 
Join Date: Mar 2008
Posts: 371
Rep Power: 6
dcs has a spectacular aura aboutdcs has a spectacular aura about
Default Re: class prototype and template error

[nm]

Last edited by dcs; 07-15-2008 at 04:20 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 07-15-2008, 05:48 PM
hellochar hellochar is offline
Newbie
 
Join Date: Jul 2008
Posts: 10
Rep Power: 0
hellochar is on a distinguished road
Default Re: class prototype and template error

Nevermind, I've figured it out. Had to use LinkedList<E>::LinkedList(). And I redeclared class LinkedList in my .cpp file, when I really just had to implement the prototypes.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 07-16-2008, 11:18 PM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 793
Last Blog:
Programs Under the Hoo...
Rep Power: 13
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default Re: class prototype and template error

Just a helpful hint you don't really need - instead of using the old #ifndef/#define trick for forcing the file to include only once, just use #pragma once before any of your code in the header file.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 07-16-2008, 11:31 PM
dcs dcs is offline
Programming Expert
 
Join Date: Mar 2008
Posts: 371
Rep Power: 6
dcs has a spectacular aura aboutdcs has a spectacular aura about
Default Re: class prototype and template error

Quote:
Originally Posted by dargueta View Post
Just a helpful hint you don't really need - instead of using the old #ifndef/#define trick for forcing the file to include only once, just use #pragma once before any of your code in the header file.
That's the less portable way.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 07-17-2008, 12:15 AM
dargueta dargueta is offline
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 793
Last Blog:
Programs Under the Hoo...
Rep Power: 13
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default Re: class prototype and template error

True, but if you're compiling on your own computer and not someone else's, it shouldn't really matter.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Tags
class, error, linked list, prototype, template



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
using Template function Chinmoy C Tutorials 4 04-03-2008 05:16 AM


All times are GMT -5. The time now is 11:17 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 101%


Complete - Celebrate!

Ads