Thread: problem with overloader bracker operator

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User cdonlan's Avatar
    Join Date
    Sep 2004
    Posts
    49

    problem with overloader bracket operator

    Hey guys ive created an overload bracket operator. For some reason I had to create an initialize function to set the array value. After it is set, the overloaded operator works fine. I would like to fix the operator so I didnt have to call an addation function before I use it. Here is my code.

    Code:
    #ifndef ASSOCARRAY_H
    #define ASSOCARRAY_H
    
    #include <iostream>
    #include <cstdlib>
    #include <string>
    
    using namespace std;
    
    template < class T >
    
    class AssocArray
    {
    	
    	struct data											
    	{
    		T value;
    		string index;
    		
    	};
    
    	public :
    		AssocArray(int size);
    		AssocArray(const AssocArray &object);
    		~AssocArray();
    		
    		T &operator[](const string &cell);
    		void initialize(const string &cell, T input);
    
    	private:
    		data *ptr;
    		int total;
    		int arraySize;
    
    };
    
    /* ===================================== */ 
    
    template< class T >											
    
    /* default AssocArray constructot */
    AssocArray < T > ::AssocArray(int inSize)
    {	
    	total = 0;
    	arraySize = inSize;
    	ptr = new data[arraySize];	
    }
    
    
    
    template< class T >											/* AssocArray copy constructor */
    AssocArray < T > ::AssocArray(const AssocArray &object)
    {
    
    	total = object.total;
    	arraySize = object.arraySize;
    	ptr = new data[arraySize];
    	for (int count = 0 ; count < arraySize ; count++)
    		*(ptr + count) = *(object.ptr + count);
    }
    
    
    
    template< class T >											/* AssocArray destructor */
    AssocArray < T > ::~AssocArray()
    {	
    }
    
    template< class T>											/* initialize values */
    void AssocArray < T > ::initialize(const string &cell, T input)
    {
    	ptr[total].index = cell;
    	ptr[total].value = input;
    	total++;
    }
    
    
    template< class T >											/* Overloaded bracket operator */
    T &AssocArray < T > ::operator[](const string &cell)
    {
    	if (cell == "")
    	{
    
    		 cout<<"no room"<<endl;
    
    		exit(1);
    	}
    	else 
    	{
    		for (int count = 0 ; count < arraySize ; count++)
    		{
    			if (ptr[count].index == cell)
    			return (ptr[count].value);
    		}
    		
    		exit(1);
    	}
    
    }
    
    #endif
    The implamentation file is empty because all the function are template related.

    My main looks like this....
    Code:
    #include <iostream>
    #include "AssocArray.h"
    
    
    using namespace std;
    int main(int argc, char *argv[])
    {
    	
    	/* Create three different data type */
    	
    	typedef AssocArray< int > intclass;
    	
    
    	intclass age(2);
    	
    	age.initialize("chris",21);
    	
    
    	age["chris"]=25;
    		
    	cout << age["chris"] << endl;
    	
    	
    
    	return 0;
    }
    If anyone see where I can change it so I can get rid of initialize function let me know.
    Thanks a lot.
    Last edited by cdonlan; 04-11-2005 at 04:56 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. two dot operator problem HELP!
    By Amyaayaa in forum C++ Programming
    Replies: 3
    Last Post: 03-13-2008, 10:45 AM
  2. oops? not sure. operator overloading (possible) problem
    By w00tw00tkab00t in forum C++ Programming
    Replies: 8
    Last Post: 02-08-2006, 05:38 AM
  3. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM
  4. Serializing problem.. (can't use >> operator)
    By RancidWannaRiot in forum Windows Programming
    Replies: 2
    Last Post: 10-29-2005, 11:10 AM
  5. problem with new operator
    By codefx in forum C++ Programming
    Replies: 4
    Last Post: 10-16-2002, 05:04 PM