Hi
In the below code , i am getting compile time error when declaring the copy constructor .

C:\work\Dev-Cpp\bin>"mingw32-c++.exe" dynamicArray.cpp
dynamicArray.cpp:13: error: expected ,' or...' before '&' token
dynamicArray.cpp:13: error: ISO C++ forbids declaration of dynarrray' with no t ype dynamicArray.cpp:13: error:dynarray::dynarray(int)' and dynarray::dynarray(in t)' cannot be overloaded dynamicArray.cpp:40: error: definition of implicitly-declareddynarray::dynarra
y(const dynarray&)'
dynamicArray.cpp:40: error: declaration of dynarray::dynarray(const dynarray&)' throws different exceptions dynamicArray.cpp:8: error: than previous declarationdynarray::dynarray(const d
ynarray&) throw ()'

#include <iostream>
#include <cstdlib>
#include <conio.h>
using namespace std ; 

class dynarray
{
      int *p ; 
      int size ;
   public:
      dynarray(int s) ;
      dynarray(const dynarrray &ob);
      ~dynarray();
      int &put(int i) ; 
      int get(int i ) ;
      dynarray &operator=(dynarray &ob) ;
      };

dynarray :: dynarray(int s) 
         {
            cout << "\t\t\t ~~~Inside normal constructor~~~\n" ; 
            size = s ; 
            if(size!=0)
            {
                p = new int[size];
                if(!p)
                {   
                  cout << "Allocation Error "  ; 
                  //exit(1) ; 
                  }
              }
              else 
                 cout << "Zero Sized Array ..Exiting program..." ;
                 //exit(1) ; 
             cout << "\t\t\t ~~~Inside normal constructor~~~\n" ; 
            } 

dynarray :: dynarray(const dynarray &ob) 
         {
            cout << "\t\t\t ~~~Inside Copy constructor~~~\n" ; 
            size = ob.size ; 
            p = new int[size] ; 
            if(!p)
            {   
                  cout << "Allocation Error "  ; 
                  //exit(1) ; 
                  }         
            for (int i=0; i < size ; i++ ) 
                p[i] = ob.p[i];
            cout << "\t\t\t ~~~Inside Copy constructor~~~\n" ; 
            }

dynarray :: ~dynarray()
         {
                       delete [] p ; 
                       cout << " Freed array memory" ; 
                       }

dynarray &dynarray :: operator=(dynarray &obj)
{
         cout << "\t\t\t ~~~Inside operator=~~~\n" ; 
         if (size != obj.size )
            {
                  cout << " Can not copy arrays of different sizes \n " ;
                  //exit (1) ; 
                  }
         for (int i=0; i < size ; i++ ) 
                p[i] = obj.p[i] ;    

         return *this ; 
         cout << "\t\t\t ~~~Inside operator=~~~\n" ;               
         }

int &dynarray :: put(int i )
         {
                if ( i<0 || size <= i )
                   {
                            cout << " Bound Error " ;
                            exit ( 1) ;
                            }
                return p[i] ; 
                }
int dynarray ::  get(int i )
         {
                if ( i<0 || size <= i )
                   {
                            cout << " Bound Error " ;
                            //exit ( 1) ;
                            }
                return p[i] ; 
                }               

void show (dynarray arr,int i )
     {
          cout << "In show() \n";

          cout << arr.get(i) << "\n" ;

     }
int main ()
    {
         int i ;
         dynarray Arr1(0) ;
         dynarray Arr2(5) ; 
         dynarray Arr3(5) ;
         dynarray Arr4(10);

         for (  i = 0 ; i < 5 ; i++ )
             Arr2.put(i) = i*i;

         for (  i = 0 ; i < 5 ; i++ )
              cout << " Arr2.get("<<i<<") ::" <<  Arr2.get(i) << " \n" ;       

         for (  i = 0 ; i < 5 ; i++ )
              show ( Arr2 , i ) ; 

        Arr3=Arr2 ; 

         for (  i = 0 ; i < 5 ; i++ )
              cout << " Arr3.get("<<i<<") ::" <<  Arr3.get(i) << " \n" ;       

         for (  i = 0 ; i < 5 ; i++ )
              show ( Arr3 , i ) ;       

        // Arr4=Arr2 ;

          cin.get() ;
          return 0 ;
          } 

Please can anyone help me with clean compilation.

Hi ,

Sorry for the trouble , i realised that dynarray is a memeber of namespace std.

I replaced it with Darray and the code compiled clean .

Thanks

Look very closely at this line dynarray(const [b]dynarrray[/b] &ob); I believe you probably meant to type dynarray instead

Hi ,

Sorry for the trouble , i realised that dynarray is a memeber of namespace std.

I replaced it with Darray and the code compiled clean .

Thanks

No it isn't, the compile error was a simple typing mistake.

Yes you are correct . Thanks a lot .
Actually , while replacing dynarray with Darray i replaced everything manually , so it worked . After ur post i switched all back to dynarray and it works...thnks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.