Problem using a privatly defined struct

This is a discussion on Problem using a privatly defined struct within the C++ Programming forums, part of the General Programming Boards category; I have this code: Code: template <class T> class SortedList { private: struct Node { T data; Node* next; }; ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    4

    Problem using a privatly defined struct

    I have this code:

    Code:
    template <class T>
    class SortedList
    {
    private:
    	struct Node
    	{
    		T data;
    		Node* next;
    	};
    	
    	Node* m_head;
    	Node* m_current;
    public:
    	friend bool operator==<T>(const SortedList<T>& L1, const SortedList<T>& L2);
    and i try to use Node in the friend function:

    Code:
    template <class T>
    bool operator==(const SortedList<T>& L1, const SortedList<T>& L2) 
    {
    	SortedList<T>::Node* head1 = L1.m_head;
    	SortedList<T>::Node* head2 = L2.m_head;
    	while (head1 != NULL && head2 != NULL) {
    		if (head1->data != head2->data) {
    			return false;
    		}
    		head1 = head1->next;
    		head2 = head2->next;
    	}
    	return (head1 == NULL && head2 == NULL);
    }
    the compiler (g++) doesn't recognize the type, although i made this function friend... i guess it's template related, but i can't find any info on how to do it with templates...

    it just says:
    Code:
    ../sortedlist.h:163: error: ‘head1’ was not declared in this scope
    ../sortedlist.h:164: error: ‘head2’ was not declared in this scope
    anyone?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You may need to indicate that operator== is a template function:
    Code:
        template <class T_>
        friend bool operator==(const SortedList<T_>& L1, const SortedList<T_>& L2);
    And then you might need the typename keyword, as you normally do for types nested within templated types:

    Code:
    	typename SortedList<T>::Node* head1 = L1.m_head;
    	typename SortedList<T>::Node* head2 = L2.m_head;
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    4
    thank you!!

    "typename" was what i've been looking for.

    the first thing you suggested isn't necessary because i predefined operator== as a template.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  2. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Struct Problem
    By Mr.Modem in forum C Programming
    Replies: 5
    Last Post: 08-13-2005, 03:19 PM
  5. Variables already defined while linking.
    By xconspirisist in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2005, 05:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21