Thread: Help me ! Link list code in OOP C++

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    8

    Help me ! Link list code in OOP C++

    This is the code wrote inOP C++ ! I don't know what error it has ? please help me !

    Code:
    #include <iostream.h>
    
    template <class T>
    //define struct link list
    struct Node
    {
    	T data;
    	Node<T>* link;
    };
    
    template <class T>
    //class link list
    class dslk
    {
    	private:
    		Node<T>* head;
    	public:
    		dslk();
    		~dslk();
    		void addhead(T);   //add to head
    		void print();
    };
    
    template <class T>
    dslk<T>::dslk()
    {
    	head = NULL;
    }
    
    template <class T>
    dslk<T>::~dslk(){}
    
    template <class T>
    dslk<T>::addhead(T key)
    {
    	Node<T>* NEW;
    	NEW = new Node<T>;
    	
    	NEW->data = key;
    	NEW->link = head;
    	head = NEW;
    }
    
    template <class T>
    dslk<T>::print()
    {
    	Node<T>* p = head;
    	cout << "Cac phan tu trong danh sach la : ";
    	while (p)
    	{
    		cout << p->data << "  ";
    		p = p->link;
    	}
    }
    
    void main(void)
    {
    	dslk<int> a;  // create a
    	a.addhead(1);
    	a.addhead(2);
    	a.addhead(3);
    	a.print();
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by phoenix2007
    This is the code wrote inOP C++ ! I don't know what error it has ? please help me !
    Perhaps post the error(s)?
    main.cpp:35: error: ISO C++ forbids declaration of `addhead' with no type
    main.cpp:35: error: prototype for `int dslk<T>::addhead(T)' does not match any in class `dslk<T>'
    main.cpp:20: error: candidate is: void dslk<T>::addhead(T)
    main.cpp:35: error: template definition of non-template `int dslk<T>::addhead(T)'
    main.cpp:46: error: ISO C++ forbids declaration of `print' with no type
    main.cpp:46: error: prototype for `int dslk<T>:rint()' does not match any in class `dslk<T>'
    main.cpp:21: error: candidate is: void dslk<T>:rint()
    main.cpp:46: error: template definition of non-template `int dslk<T>:rint()'
    main.cpp:57: error: `main' must return `int'
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    8

    Thumbs up

    Code:
    error C2244: 'dslk<T>::addhead' : unable to resolve function overload
    error C2954: template definitions cannot nest
    error C2244: 'dslk<T>::print' : unable to resolve function overload
    error C2065: 'a' : undeclared identifier
    error C2228: left of '.addhead' must have class/struct/union type
    error C2228: left of '.addhead' must have class/struct/union type
    error C2228: left of '.addhead' must have class/struct/union type
    error C2228: left of '.print' must have class/struct/union type
    Here are errors ! Please help resolve them ! I can't ! thanks a lots !

    (These code were wrote in VS C++ 6.0)
    Last edited by phoenix2007; 01-16-2007 at 04:18 AM.

  4. #4
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Code:
    #include <iostream.h>
    Wrong.
    Code:
    #include <iostream>
    Code:
    void main(void)
    Wrong.
    Code:
    int main ()
    If you don't know these simple porblems, what makes you think you can take on templates and classes?
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    8

    Smile

    Quote Originally Posted by manutd
    Code:
    #include <iostream.h>
    Wrong.
    Code:
    #include <iostream>
    Code:
    void main(void)
    Wrong.
    Code:
    int main ()
    If you don't know these simple porblems, what makes you think you can take on templates and classes?
    Oh ! I wrote
    Code:
    void main(void)
    It' s not wrong ! Because, in main functiuon, if I write follow you ! i have to return a value, That's thing I don't want !
    Code:
     #include "iostream.h"
    instead of #include <iostream>. They are the same ! No fault !

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by phoenix2007
    Code:
    void main(void)
    It' s not wrong !
    It's wrong. Read the FAQ.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    8
    oh ! I'm sorry ! I've just remember !
    Code:
    void main(void)
    just right in C, not in C++. If in C++, must write :
    Code:
     
    void main()
    Is that ok ?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It' s not wrong ! Because, in main functiuon, if I write follow you ! i have to return a value, That's thing I don't want !
    It's wrong in that it is non-standard. Declare main() with a return type of int. You still do not need to return a value explicitly as the global main() function is special in that it will return 0 if control reaches its end and no return statement is encountered.

    instead of #include <iostream>. They are the same ! No fault !
    <iostream.h> is pre-standard. The standard header is <iostream>. "iostream.h" would be some user defined header.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    Yes you are wrong with iostream.h and void main. Newer compilers dont have iostream.h anymore.

    Anyway, try rewriting the complete templates in the header file and see what that fixes

  10. #10
    Registered User
    Join Date
    Jan 2007
    Posts
    8
    Yes, thanx all ! I fixed them follow you ! but, it still has 8 errors !

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    it still has 8 errors !
    What might they be?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Jan 2007
    Posts
    8
    Quote Originally Posted by laserlight
    What might they be?
    error C2244: 'dslk<T>::addhead' : unable to resolve function overload
    error C2954: template definitions cannot nest
    error C2244: 'dslk<T>:rint' : unable to resolve function overload
    error C2065: 'a' : undeclared identifier
    error C2228: left of '.addhead' must have class/struct/union type
    error C2228: left of '.addhead' must have class/struct/union type
    error C2228: left of '.addhead' must have class/struct/union type
    error C2228: left of '.print' must have class/struct/union type

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by phoenix2007
    oh ! I'm sorry ! I've just remember !
    Code:
    void main(void)
    just right in C, not in C++. If in C++, must write :
    Code:
     
    void main()
    Is that ok ?
    no no No NO NO!
    That has never been valid C or C++.
    Note that in this special case, even though it must be int main, you do not have to have a return statement.
    Read the relevant part of this:
    http://www.research.att.com/~bs/bs_faq2.html

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You are missing your return type (which is void) on the function definitions that cause the errors.

  15. #15
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    aka
    Code:
    template <class T>
    dslk<T>::addhead(T key)
    {
    	Node<T>* NEW;
    	NEW = new Node<T>;
    	
    	NEW->data = key;
    	NEW->link = head;
    	head = NEW;
    }
    Should be:
    Code:
    template <class T>
    void dslk<T>::addhead(T key)
    {
    	Node<T>* NEW;
    	NEW = new Node<T>;
    	
    	NEW->data = key;
    	NEW->link = head;
    	head = NEW;
    }
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. stack error occured in link list
    By mycount in forum C Programming
    Replies: 5
    Last Post: 07-11-2006, 09:03 PM
  4. Simple list code
    By JimpsEd in forum C Programming
    Replies: 1
    Last Post: 05-24-2006, 02:01 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM