Passing Array of Object gives compilation ERROR
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Passing Array of Object gives compilation ERROR

Hybrid View

  1. #1
    Join Date
    Mar 2010
    Posts
    6

    Passing Array of Object gives compilation ERROR

    Hello Everybody

    This is my question
    Define a class named HOUSING in C++ with the following descriptions:
    Private members
    REG_NO integer(Ranges 10 1000)
    NAME Array of characters(String)
    TYPE Character
    COST Float
    Public Members
    Function Read_Data( ) to read an object of HOUSING type
    Function Display() to display the details of an object
    Function Draw Nos( ) to choose and display the details of 2 houses selected randomly from an array of 10 objects of type HOUSING Use random function to generate the registration nos. to match with REGNO from the array.

    Now I' trying to do this by this way
    Code:
          #include <iostream.h>
          #include <conio.h>
          #include <stdlib.h>
        
          class housing
          {
                private:
                    int REG_NO;
                    char NAME[10];
                    char TYPE;
                    float COST;
                public:
                    void Read_Data();
                    void Display();
                    void DrawNos(housing);
          };
          void housing::Read_Data( )
          {
                cout<<"Enter Registration No: ";
                cin>>REG_NO;
                cout<<"Enter Name: ";
                cin>>NAME;
                cout<<"Enter Type: ";
                cin>>TYPE;
                cout<<"Enter cost: ";
                cin>>COST;
          }
          void housing::Display()
          {
          }
          void housing::DrawNos(housing* h1[])
          {
               int N=10;
               int randomREG=random(10);
               N=random(2);
               cout<<h1[N]->REG_NO;
          }
          void main()
          {     
                int i=0;
                housing* h[5];
                for(i=0;i<5;i++)
                {
                        h[i]->Read_Data();
                }
                for(i=0;i<5;i++)
                {
                        h[i].DrawNos(h); // I am trying to pass the array of object to DrawNos function but getting error
                }
          }
    I am tryin to pass the entire array of object in DrawNos(). but getting comilation error -

    32: 'housing:rawNos(housing * *)' is not a member of 'housing'
    48: Structure required on left side of . or .*
    What is the problem? How can I pass the array of object in function and use it.

    Please help me with this problem.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,202

    Re: Passing Array of Object gives compilation ERROR

    It seems to me that if DrawNos is to be a member function, then it should be a static member function. Otherwise, it should be a non-member function. It does not make sense for it to be a non-static member function because it deals with an array of housing objects without involving the current housing object.

    As for your problem: look carefully at the parameter. You wrote: housing* h1[]

    You probably wanted to write: housing h1[]
    or: housing* h1
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Mar 2010
    Posts
    6

    Re: Passing Array of Object gives compilation ERROR

    OK
    Now I have this code very simple and plain which is giving fine output. As I am passing the array of object in showAge function.
    Code:
    #include <iostream.h>
    #include <conio.h>
    class fish
    {
         private:
             int age;
         public:
             int getAge() 
             {
                   return age;
             }
             void setAge(int newage)
             {
                    age = newage;
             }
    };
    void showAges(fish fishes[])   // ERROR FROM THIS LINE
    {
           cout << "fish 1 age is: " << fishes[0].getAge() << endl;
           cout << "fish 2 age is: " << fishes[1].getAge() << endl;
           cout << "fish 3 age is: " << fishes[2].getAge() << endl;
    }
    int main()
    {
    fish myFish[3];
    clrscr();
    showAges(myFish);
    cout << endl;
    myFish[0].setAge(10);
    myFish[1].setAge(20);
    myFish[2].setAge(30);
    showAges(myFish);
    cin.get();
    return 0;
    }
    But if make this function as a member of the class it gives compilation error of 'Undefined structure fish'

  4. #4
    Join Date
    Apr 1999
    Posts
    27,418

    Re: Passing Array of Object gives compilation ERROR

    Quote Originally Posted by vishalon View Post
    But if make this function as a member of the class it gives compilation error of 'Undefined structure fish'
    That is because you're using a compiler that is either so old, or so non-standard that the code doesn't compile. I know this since you're including <iostream.h>.

    There is no <iostream.h> in ANSI C++, as the correct header is <iostream>. What compiler are you using?

    This code compiles with no issues:
    Code:
    #include <iostream>
    class fish
    {
         private:
             int age;
         public:
             int getAge() 
             {
                   return age;
             }
             void setAge(int newage)
             {
                    age = newage;
             }
    
             void showAges(fish fishes[])   
            {  }
    };
    
    
    int main()
    { 
      fish f;
      f.showAges(&f);
    }
    The code compiles correctly with a standard ANSI C++ compiler. However, there are problems when you run it. How do you know that there are at least 3 items in the array when you call the function showAges? What if I call showAges() as I did above, where there is only 1 fish?

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center