Thread: Linked List Queue Implementation help

  1. #1
    C++ novice
    Join Date
    Sep 2005
    Location
    Washington State
    Posts
    5

    Linked List Queue Implementation help

    I have been studying C++ for a year now, and I've started a small test project: A linked list container library. I'm using Dev-C++, and when I try to compile the listing, it gives me the errors:
    Compiler: Default compiler
    Executing g++.exe...
    g++.exe "C:\Documents and Settings\Stephen\Desktop\Nathan\Programming Languages\C++\Kenogu's Container Library\QueueTest.cpp" -o "C:\Documents and Settings\Stephen\Desktop\Nathan\Programming Languages\C++\Kenogu's Container Library\QueueTest.exe" -fexceptions -pg -g3 -I"C:\Program Files\Dev C++\Dev-Cpp\include\c++" -I"C:\Program Files\Dev C++\Dev-Cpp\include\c++\backward" -I"C:\Program Files\Dev C++\Dev-Cpp\include" -I"C:\Program Files\Dev C++\Dev-Cpp\include\c++\mingw32" -I"C:\Program Files\Dev C++\Dev-Cpp\include\c++\random" -L"C:\Program Files\Dev C++\Dev-Cpp\lib" -L"C:\Program Files\Dev C++\Dev-Cpp\Allegro\Lib"
    In file included from C:/Documents and Settings/Stephen/Desktop/Nathan/Programming Languages/C++/Kenogu's Container Library/Queue.cpp:12,
    from C:/Documents and Settings/Stephen/Desktop/Nathan/Programming Languages/C++/Kenogu's Container Library/QueueTest.cpp:2:
    C:/Documents and Settings/Stephen/Desktop/Nathan/Programming Languages/C++/Kenogu's Container Library/Queue.hpp:26: `

    LLQueue' is not a template type
    C:/Documents and Settings/Stephen/Desktop/Nathan/Programming Languages/C++/Kenogu's Container Library/Queue.hpp:34: data
    member `head' cannot be a member template
    C:/Documents and Settings/Stephen/Desktop/Nathan/Programming Languages/C++/Kenogu's Container Library/Queue.hpp:35: data
    member `tail' cannot be a member template
    C:/Documents and Settings/Stephen/Desktop/Nathan/Programming Languages/C++/Kenogu's Container Library/Queue.hpp:36: data
    member `size' cannot be a member template
    In file included from C:/Documents and Settings/Stephen/Desktop/Nathan/Programming Languages/C++/Kenogu's Container Library/QueueTest.cpp:2:
    C:/Documents and Settings/Stephen/Desktop/Nathan/Programming Languages/C++/Kenogu's Container Library/Queue.cpp:19: non-template
    type `LLQueue' used as a template
    C:/Documents and Settings/Stephen/Desktop/Nathan/Programming Languages/C++/Kenogu's Container Library/Queue.cpp:20: ISO
    C++ forbids declaration of `LLQueue' with no type
    C:/Documents and Settings/Stephen/Desktop/Nathan/Programming Languages/C++/Kenogu's Container Library/Queue.cpp: In
    function `int LLQueue()':
    C:/Documents and Settings/Stephen/Desktop/Nathan/Programming Languages/C++/Kenogu's Container Library/Queue.cpp:20: only
    constructors take base initializers
    C:/Documents and Settings/Stephen/Desktop/Nathan/Programming Languages/C++/Kenogu's Container Library/Queue.cpp: At
    global scope:
    C:/Documents and Settings/Stephen/Desktop/Nathan/Programming Languages/C++/Kenogu's Container Library/Queue.cpp:23: syntax
    error before `::' token
    C:/Documents and Settings/Stephen/Desktop/Nathan/Programming Languages/C++/Kenogu's Container Library/Queue.cpp:23: `
    t' was not declared in this scope
    C:/Documents and Settings/Stephen/Desktop/Nathan/Programming Languages/C++/Kenogu's Container Library/Queue.cpp:23: `
    InData' was not declared in this scope
    C:/Documents and Settings/Stephen/Desktop/Nathan/Programming Languages/C++/Kenogu's Container Library/Queue.cpp:23: ISO
    C++ forbids declaration of `push' with no type

    C:/Documents and Settings/Stephen/Desktop/Nathan/Programming Languages/C++/Kenogu's Container Library/Queue.cpp:23: syntax
    error before `{' token
    C:/Documents and Settings/Stephen/Desktop/Nathan/Programming Languages/C++/Kenogu's Container Library/Queue.cpp:27: ISO

    C++ forbids declaration of `tail' with no type
    C:/Documents and Settings/Stephen/Desktop/Nathan/Programming Languages/C++/Kenogu's Container Library/Queue.cpp:27: `
    node' was not declared in this scope
    C:/Documents and Settings/Stephen/Desktop/Nathan/Programming Languages/C++/Kenogu's Container Library/Queue.cpp:28: parse
    error before `if'
    C:/Documents and Settings/Stephen/Desktop/Nathan/Programming Languages/C++/Kenogu's Container Library/Queue.cpp:30: syntax

    error before `++' token
    C:/Documents and Settings/Stephen/Desktop/Nathan/Programming Languages/C++/Kenogu's Container Library/Queue.cpp:34: syntax
    error before `::' token
    C:/Documents and Settings/Stephen/Desktop/Nathan/Programming Languages/C++/Kenogu's Container Library/Queue.cpp:34: ISO
    C++ forbids declaration of `pop' with no type
    C:/Documents and Settings/Stephen/Desktop/Nathan/Programming Languages/C++/Kenogu's Container Library/Queue.cpp: In
    function `int pop()':
    C:/Documents and Settings/Stephen/Desktop/Nathan/Programming Languages/C++/Kenogu's Container Library/Queue.cpp:35: `
    size' undeclared (first use this function)
    C:/Documents and Settings/Stephen/Desktop/Nathan/Programming Languages/C++/Kenogu's Container Library/Queue.cpp:35: (Each
    undeclared identifier is reported only once for each function it appears
    in.)

    C:/Documents and Settings/Stephen/Desktop/Nathan/Programming Languages/C++/Kenogu's Container Library/Queue.cpp:37: `
    t' undeclared (first use this function)
    C:/Documents and Settings/Stephen/Desktop/Nathan/Programming Languages/C++/Kenogu's Container Library/Queue.cpp:37: parse
    error before `=' token
    C:/Documents and Settings/Stephen/Desktop/Nathan/Programming Languages/C++/Kenogu's Container Library/Queue.cpp:38: `

    LLQueueNode' undeclared (first use this function)
    C:/Documents and Settings/Stephen/Desktop/Nathan/Programming Languages/C++/Kenogu's Container Library/Queue.cpp:39: `
    head' undeclared (first use this function)
    C:/Documents and Settings/Stephen/Desktop/Nathan/Programming Languages/C++/Kenogu's Container Library/Queue.cpp:40: `
    newHead' undeclared (first use this function)
    C:/Documents and Settings/Stephen/Desktop/Nathan/Programming Languages/C++/Kenogu's Container Library/Queue.cpp:42: `
    data' undeclared (first use this function)
    C:/Documents and Settings/Stephen/Desktop/Nathan/Programming Languages/C++/Kenogu's Container Library/QueueTest.cpp: In
    function `int main()':
    C:/Documents and Settings/Stephen/Desktop/Nathan/Programming Languages/C++/Kenogu's Container Library/QueueTest.cpp:6: parse
    error before `(' token
    C:/Documents and Settings/Stephen/Desktop/Nathan/Programming Languages/C++/Kenogu's Container Library/QueueTest.cpp:11: `
    q' undeclared (first use this function)

    Execution terminated
    Here is the code used:
    Code:
    /*
    		 			-=Linked List queue=-
    				 -=COPYRIGHT KENOGU LABZ 2005=-
    
    Classes used:
    LLQueue
    LLQueueNode
    */
    #ifndef QUEUE_HPP
    #define QUEUE_HPP
    
    class LLQueue;
    
    template <class t>
    class LLQueueNode {
    public:
    	LLQueueNode(t * InData, LLQueueNode * InNext);
    	~LLQueueNode(){delete data; delete next;}
    	friend class LLQueue;
    private:
    	t * data;
    	LLQueueNode<t> * next;
    };
    
    template <class t>
    class LLQueue {
    public:
    	LLQueue();
    	~LLQueue(){delete head; delete tail;}
    	void push(t * InData);
    	t * pop();
    	int getSize() {return size;}
    private:
    	LLQueueNode<t> * head;
    	LLQueueNode<t> * tail;
    	int size;
    };
    
    class Empty{};
    
    #endif
    -------------------------------------------------------------------
    
    /*
    		 			-=Linked List queue=-
    				 -=COPYRIGHT KENOGU LABZ 2005=-
    
    Classes used:
    Queue
    QueueNode
    */
    #ifndef QUEUE_CPP
    #define QUEUE_CPP
    
    #include "Queue.hpp"
    
    template <class t>
    LLQueueNode<t>::LLQueueNode(t * InData, LLQueueNode * InNext)
    	:data(InData), next(InNext){}
    
    template <class t>
    LLQueue<t>::LLQueue()
    	:head(0), tail(0), size(0){}
    
    template <class t>
    LLQueue<t>::push(t * InData) {
    	LLQueueNode* node = new LLQueueNode(InData, 0);
    	if(size)
    		tail->next = node;
    	tail = node;
    	if(!size)
    		head = node;
    	size++;
    }
    
    template <class t>
    t LLQueue<t>::pop() {
    	if(!size)
    		throw Empty();
    	t data = head->data;
    	LLQueueNode newHead = head->next;
    	delete head;
    	head = newHead;
    	size--;
    	return data;
    }
    -----------------------------------------------------------------
    
    #include <iostream>
    #include "Queue.cpp"
    using namespace std;
    
    int main() {
    	LLQueue<int> q();
    	unsigned short choice;
    	 do {
    		 cout << "Enter a number to add to the queue (0 to exit): ";
    		 cin >> choice;
    		 q.push(new int(choice));
    	 } while(choice!=0);
    	 try {
    		 cout << "Queue Size: " << q.getSize() << endl;
    		 cout << "Next value: " << q.pop() << endl;
    	}
    	catch(Empty) {}
    	cout << "End of Queue. (Press CTRL-Z)";
    	char leave;
    	do
    		cin >> leave;
    	while(leave != EOF);
    	return 0;
    }
    Could anyone tell me why this isn't compiling? Design criticisms are welcome, too.

    -=Kenogu Labz=-

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    The vast majority of your errors are because you declare LLQueue as a simple class by saying:
    Code:
    class LLQueue;
    but then later on when you implement it you say its a template class. Change the line to:
    Code:
    template <class t> class LLQueue;
    Also, you can't have a line of dashes without commenting them out.

    [edit]nevermind, i'm guessing you just added the lines to distinguish between files[/edit]
    Last edited by PJYelton; 09-20-2005 at 01:24 PM.

  3. #3
    C++ novice
    Join Date
    Sep 2005
    Location
    Washington State
    Posts
    5
    Wow, that really helped. Didn't know that prototypes had to be declared as templates, too (although it makes perfect sense.) Now there's only 19 errors left. :P
    The only other thing that's bugging me is the constant "ISO forbids declaration of 'push' with no type" message. Is that saying it is neccessary to have a return type?

  4. #4
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    You have several errors because you are using the template without declaring the type, for example:
    Code:
    LLQueueNode* node = new LLQueueNode(InData, 0);
    should be
    Code:
    LLQueueNode<t>* node = new LLQueueNode<t>(InData, 0);
    Also, you are routinely are declaring variables as pointers and then later on using them as non-pointers, such as trying to print a pointer to an integer.

    The error you are talking about with push() is because you forgot to put "void" when defining it.

  5. #5
    C++ novice
    Join Date
    Sep 2005
    Location
    Washington State
    Posts
    5
    Lol, I'm too sloppy for my own good. Thanks alot for your help, again!
    And I'm pretty sure this is the last problem; I fixed all but 3 errors; they say:
    C:/Documents and Settings/*****/Desktop/*****/Programming Languages/C++/Kenogu's Container Library/QueueTest.cpp:11: request
    for member `push' in `q()', which is of non-aggregate type `LLQueue<int>
    ()()'
    C:/Documents and Settings/*****/Desktop/*****/Programming Languages/C++/Kenogu's Container Library/QueueTest.cpp:14: request
    for member `getSize' in `q()', which is of non-aggregate type `LLQueue<int>
    ()()'
    C:/Documents and Settings/*****/Desktop/*****/Programming Languages/C++/Kenogu's Container Library/QueueTest.cpp:15: request
    for member `pop' in `q()', which is of non-aggregate type `LLQueue<int> ()()
    Okay, could you tell me what that error is supposed to mean? I'll try to remember all these for future reference; at the moment, it's all mumbo jumbo, though.
    Last edited by Kenogu Labz; 09-21-2005 at 12:40 PM.

  6. #6
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Get rid of the () in this line and see if it helps:
    Code:
    LLQueue<int> q();

  7. #7
    C++ novice
    Join Date
    Sep 2005
    Location
    Washington State
    Posts
    5
    WOAH, that didn't work at all. Gave me 25 more errors. What is the error message implying?

  8. #8
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Post the new code, I've never seen the error before but I'm pretty sure its because you have the unnecessary parenthesis when declaring q.

  9. #9
    C++ novice
    Join Date
    Sep 2005
    Location
    Washington State
    Posts
    5
    PROBLEM SOLVED! Thanks for all your help, PJYelton. ;)
    Last edited by Kenogu Labz; 09-21-2005 at 12:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list question
    By mikeman in forum C Programming
    Replies: 1
    Last Post: 11-30-2008, 01:56 PM
  2. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  3. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM
  4. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 07:11 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM