Thread: problem with overloader bracker operator

  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.

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    The subscript operator should return a reference to the element specified by the key, and if that element does not exist, it should be inserted, which means you need to grow your array to make room for it.

  3. #3
    Registered User cdonlan's Avatar
    Join Date
    Sep 2004
    Posts
    49
    Well,l im working on an exercise using exception handling. So I want it to be a fixed size so I can cause it to have a run-time error.

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Then throw an error when your class is asked to insert another object when it's full.

  5. #5
    Registered User cdonlan's Avatar
    Join Date
    Sep 2004
    Posts
    49
    I understand that, but how to I prevent having to initialize each element.

  6. #6
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    What I was trying to say before is that the work of making room for an element should be done in the operator[] function or in a private function called by it. Come to think of it, why not call your initialization function inside operator[]?

  7. #7
    Registered User cdonlan's Avatar
    Join Date
    Sep 2004
    Posts
    49
    Cause then you would reinitialize it everytime you used that operator.

  8. #8
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    You need only initialize when that key isn't yet an element in the associative array.

  9. #9
    Registered User cdonlan's Avatar
    Join Date
    Sep 2004
    Posts
    49
    Regardless of where I call the initialize function it will still get an error in the function. I need throw it in the operator function.
    Last edited by cdonlan; 04-11-2005 at 02:25 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