template class and its members?

This is a discussion on template class and its members? within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm having a bit of strange problem that I can't seem to figure out, or maybe my eyes are ...

  1. #1
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279

    template class and its members?

    Hi, I'm having a bit of strange problem that I can't seem to figure out, or maybe my eyes are just too tired from looking at this code, anyway...
    The compiler, I think is trying to say that perhaps a syntax error of some sort exists somewhere in that area...???
    I have a class template within i overload the '<<' by writing the follwing:

    Code:
    //in List.h file...
    
    friend ostream& operator <<(ostream& outs, const List<ItemType>& the_list);
    		// Overloads the operator '<<' so that the contents of the List can
    		// be outputed
    and inside the implementation file i write the following:

    Code:
    template<class ItemType>
    ostream& operator <<(ostream& outs, const List<ItemType>& the_list)
    {
    	for(int i = 0; i < the_list.length(); i++)
    		outs << the_list.item[i] << endl;
    
    	return outs;
    }
    the compiler directs me to the declaration of that function as the source of the problem, I have been looking at this for a while but i can't find the problem...
    it's telling me some stupid stuff about putting ';' in front of '&'

    weird...

    any ideas???

    thanx

    matheo917

    (if you guys need me to post more code then let me know)

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    that wasn't the problem, b/c that function declaration is already a part of class which in fact is a template itself...

    like i said before, probably it was a stupid problem and it was, i didn't include <iostream> in my ".h" file.....

    however i have a different problem...

    in the function definition (which is declared as a "friend")
    i get an an error that I can't acces privated data members by writing:

    Code:
     outs << the_list.item[i] << endl;
    However, this function is declared to be a FRIEND, and friends can acces private data members directly???

    hmmm...what's going on???


    thanx

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    If you are using microsoft visual c++ 6 this is a bug. If you define the function inside the class definition all should be well.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    thanx, i guess i can blame this one on Microsoft...nice...
    everything compiles and links fine, however the program crashes, ....the program is all nice and good (logically) as far as I'm concerned..... my eyes are done for today...

    if any of you would like to take a peek be my guest,
    the following are 3 small source code files:
    maybe you can run it....or maybe tomorrow I will say Hi again to Borland...

    Code:
    ................
    // List.h    (class template)
    
    //---------------------------------------------------------------------------------------
    
    #include<iostream>
    using namespace std;
    
    #ifndef LIST_H
    #define LIST_H
    
    template<class ItemType>
    class List
    {
    	public:
    		List(int max);
    		// Initializes the object to an empty list that can hold up to the 'max'
    		// items of the type 'ItemType' ...
    
    		~List();
    		// Returns all the dynamically allocated memory to the freestore.
    
    		int length() const;
    		// Returns the number of items in the list.
    
    		void add(ItemType new_item);
    		// Preconditions:  List must NOT be full!!!
    		// Postconditions: The 'new_item' has been added to the list.
    
    		bool full() const;
    		// Returns 'true' if the list is full and 'false' if the list is not.
    
    		void erase();
    		// Removes all the items from the list so the list is empty...
    
    		friend ostream& operator <<(ostream& outs, const List<ItemType>& the_list)
    		{
    			for(int i = 0; i < the_list.current_length; i++)
    			outs << the_list.item[i] << endl;
    
    			return outs;
    		}
    
    		// Overloads the operator '<<" so that the contents of the List can
    		// be outputed 
    
    	private:
    		ItemType *item;		// pointer to the dynamically allocated array
    		int max_length;		// max number of items allowed in the list
    		int current_length; // number of items currently in the list
    };
    
    
    #endif
    now it's implementation:
    Code:
    // List.cpp 
    //---------------------------------------------------------------------------------------
    
    #include<iostream>
    #include<cstdlib>
    using namespace std;
    
    
    
    template<class ItemType>
    List<ItemType>::List(int max): max_length(max)
    {
    	item = new ItemType[max];
    }
    
    
    
    template<class ItemType>
    List<ItemType>::~List()
    {
    	delete [] item;
    }
    
    
    
    template<class ItemType>
    int List<ItemType>::length() const
    {
    	return current_length;
    }
    
    
    
    template<class ItemType>
    void List<ItemType>::add(ItemType new_item)
    {
    	if( full() )
    	{
    		cout << "The LIST is full.  Exiting the program!!! \n";
    		exit(1);
    	}
    	
    	else
    	{
    		item[current_length] = new_item;
    		current_length++;
    	}
    }
    
    
    
    template<class ItemType>
    bool List<ItemType>::full() const
    {
    	return (current_length == max_length);
    }
    
    
    
    template<class ItemType>
    void List<ItemType>::erase()
    {
    	current_length = 0;
    }
    
    
    //---------------------------------------------------------------------------------------
    and finally a tiny 'main' function
    Code:
    // driver function...
    
    //---------------------------------------------------------------------------------------
    
    #include<iostream>
    #include"List.h"
    #include"List.cpp"
    using namespace std;
    
    
    int main()
    {
    	List<int> int_list(2);
    	
    	int_list.add(2);
    	int_list.add(4);
    
    
    	cout << "List of Integers: \n";
    	cout << int_list;
    
    
    
    	List<char> char_list(3);
    	
    	char_list.add('A');
    	char_list.add('B');
    	char_list.add('C');
    
    	cout << "\nList of Characters: \n";
    	cout << char_list;
    
    	return 0;
    }
    
    //---------------------------------------------------------------------------------------

    have fun....

    sorry for a long post...

    matheo917

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    constructor doesnt initialise current_length so its first use in the add function crashes u.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

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