Link to home
Start Free TrialLog in
Avatar of rmvprasad
rmvprasad

asked on

Accessing of objects in an array

I have an template which is an array that stores objects. To display all the objects of the array I wrote a code it is not working.  The code is as below. The displaying of the trucks is written in Agency class.

cout<<trucks; //code to display the trucks


trucks is a template of array to store trucks

FlexArray<Trucks*> trucks;// declaration of trucks template.(this declaration is also done in agenct class only)

The code for trucks and the template(Flexarray) is as below.
/////////////////////////////////////trucks.cpp//////////////////////////////////////////////////////////////

#include "Trucks.hpp"

Trucks::Trucks(char *a,int b,int c,bool avail)
{
      int len;
      len = strlen(a);

    Truck_Name = new char[len+1];
      
      strcpy(Truck_Name,a);
      Year=b;
      VIN=c;
      Available=avail;
}
      
Trucks::~Trucks()
{
   clog<<"Deleted Truck"<<Truck_Name<<endl;
   delete Truck_Name;

}

ostream& Trucks::print(ostream& out)
{
    if(Available)
            out<<setw(10)<<VIN<<setw(20)<<Truck_Name<<setw(10)<<Year<<setw(6)<<"Yes"<<endl;
      else
        out<<setw(10)<<VIN<<setw(20)<<Truck_Name<<setw(10)<<Year<<setw(6)<<"No"<<endl;
    return out;
}


////////////////////////////////flexarray.hpp/////////////////////////////////////////////////////////////////////

#ifndef FLEX
#define FLEX
 // ==========================================================================
 // Template declaration for a flexible array of base type T.

 #include "tools.hpp"
 #define FLEX_START 4 // Default length for initial array.

 template <class T>
 class FlexArray {
 private:// ---------------------------------------------------------------
 int Max;                  // Current allocation size.
 int N;                        // Number of array slots that contain data.
 T* Data;                  // Pointer to dynamic array of T.

 void grow(void);            // Double the allocation length.

 public: // ---------------------------------------------------------------
 FlexArray( int ss = FLEX_START ) : Max(ss), N(0), Data( new T[Max] ) {}
 //~FlexArray() { if (Data != NULL) delete[] Data; }
 ~FlexArray() { for (int i=0; i<N; i++) delete Data[i]; }

 int put( T data );
 const T find(int id){
       for(int i=0; i<N; ++i) {if(*Data[i]==id) return Data[i];}
       return NULL;

 }

 const T find(char* nm){
       for(int i=0; i<N; ++i) {if(*Data[i]==nm) return Data[i];}
       return NULL;

 }
 
T remove(int id)
{
      T temp;

      for(int i=0;i<N; i++)
      {
            if(*Data[i]==id) {temp=Data[i]; Data[i]=Data[--N]; delete temp;}
      }

      return NULL;
}

 T& operator[]( int k );

 int data_size() {return N;}

 T* extract() { T* tmp=Data; Data=NULL; Max = N = 0; return tmp; }
 ostream& print( ostream& out ) const
 {
      for (int k=0; k<N; ++k) out << Data[k] <<endl;
      return out;

 }
 };
 template <class T> inline ostream&
 operator<< ( ostream& out, FlexArray<T>& bp){ return bp.print(out); }

template <class T> int
 FlexArray<T>::put( T data ){
 if ( N >= Max ) grow(); // Create more space if necessary.
                        
 Data[N]= data;
 return ++N;            // Return subscript at which item was stored.
 }

 //------------------------------------------- access the kth T in the array.
 template <class T> T&
 FlexArray<T>::operator[]( int k ) {
 if ( k >= N ) fatal( "Flex_array bounds error." );
 return Data[k];      // Return reference to desired array slot.
 }

 // ------------------------------------- double the allocation length.
 template <class T> void
 FlexArray<T>::grow()
 {
      T* temp = Data;    // hang onto old data array.
      Max*=2 ;
      Data = new T[Max]; // allocate a bigger one.
      memcpy(Data, temp, N*sizeof(T)); // copy info into new array.
      delete temp;            // recycle (free) old array.
 }                  
 
#endif

Avatar of andrewjb
andrewjb
Flag of United Kingdom of Great Britain and Northern Ireland image

Give us a clue. In what way doesn't it work?
Avatar of smpoojary
smpoojary

Don't show your actual code. Becuase we have no patience to read it.  
Please convert it to a simple example. After clear this simple example then you can convert it to your actual project code.

Let me know simple example for your doubt.
You have an array of pointers to objects. That means that T is of type Trucks*.

You find the object with one of the following:
--------8<--------
//...
const T find(int id){
      for(int i=0; i<N; ++i) {if(*Data[i]==id) return Data[i];}
      return NULL;
 }
const T find(char* nm){
      for(int i=0; i<N; ++i) {if(*Data[i]==nm) return Data[i];}
      return NULL;

 }
--------8<--------

That means that you would need to have Trucks::operator==(int) or Trucks::operator==(const char*) defined, which I do not see in your Trucks class definition. In any case I don't believe that it would be good style to have one of these defined.

May I suggest you take a step back and reconsider your design.

Is this an academic assignment or can you consider using a standard library container?
ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial