Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Passing Array of Object gives compilation ERROR (http://www.go4expert.com/forums/passing-array-object-compilation-error-t29365/)

vishalonne 21Dec2012 22:25

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'm 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 trying to pass the entire array of object in DrawNos(). but getting comilation error -

Quote:

32: 'housing::DrawNos(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.

xpi0t0s 23Dec2012 01:09

Re: Passing Array of Object gives compilation ERROR
 
Code:

housing* h[5];
defines h as an array of 5 pointers to housing objects. You don't define the memory for those objects anywhere; these are only pointers (and so Read_Data is writing that data somewhere completely undefined in memory). Probably you meant to do:

Code:

housing h[5];
so I've fixed that in my copy of your code. This now means that h[x]->y has to be h[x].y so I'll fix that too.

Your DrawNos() seems to want to take an array of pointers to housing objects for some reason, yet doesn't loop over that array. You seem to be confused about syntax. I'll change this to:

Code:

void housing::DrawNos(housing h1)
So now we get to your problem:
Code:

h[i].DrawNos(h); // I am trying to pass the array of object to DrawNos function but getting error
I'm not surprised. Why are you trying to pass the whole array when you are already looping over that array? What you need to do is to loop over the array and pass one object at a time into a function that handles one object (which is what DrawNos is written to do, even given the syntax errors), OR not to loop over the array and pass the whole array into a function which then loops over that array.

Also I don't see any reason for passing h into the function. h[i] already identifies the object you're working on, so just pass nothing.

I can't see what you're trying to do in DrawNos. Could you explain it please? Give me an overall description of what you want the function to do, then explain what each line contributes to that aim. There are only 4 lines so this shouldn't be too difficult. As I don't know what you're trying to do I can't fix it; the code is just too random.

vishalonne 23Dec2012 14:27

Re: Passing Array of Object gives compilation ERROR
 
thank you xpi0t0s but now I am fed up with this question. I am totally completely confused :crazy: how can I get the ALL REGNO for randomizing them are show the selected REGNO.
If you can show mw some way I will be thankful

vishalonne 23Dec2012 14:31

Re: Passing Array of Object gives compilation ERROR
 
Here the question
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.

I hope you gone through the question. I am try to gerenerate creating the array of objects and then select any 2 random the REGNO from those REGNO. Before selecting the REGNO I have to enter the details.
I lost my way:(

xpi0t0s 23Dec2012 22:52

Re: Passing Array of Object gives compilation ERROR
 
OK it's probably easiest if I give you an example.
Code:

class housing
{
private:
        int REG_NO;
        char NAME[10];
        char TYPE;
        float COST;
public:
        void Read_Data(int i);
        void Display();
        void DrawNos(housing h[]);
};

void housing::Read_Data(int i)
{
        REG_NO=10*i;
        sprintf_s(NAME,10,"name%d",i);
        TYPE='a'+i;
        COST=i*100;
}

void housing::Display()
{
}

void housing::DrawNos(housing h[])
{
        int rnd1=rand()%10;
        int rnd2=rand()%10;
        while (rnd2==rnd1)
        {
                rnd2=rand()%10;
        }
        printf("House %d's name is %s\n",rnd1,h[rnd1].NAME);
        printf("House %d's name is %s\n",rnd2,h[rnd2].NAME);
}

void test56()
{   
        int i=0;
        housing h[10];
        for(i=0;i<10;i++)
        {
                h[i].Read_Data(i);
        }
        h[0].DrawNos(h);
}


xpi0t0s 23Dec2012 22:53

Re: Passing Array of Object gives compilation ERROR
 
This is done in a testbed so my main function calls test56(). You might want to rename test56 to main. I couldn't be bothered typing stuff in so I autogenerated the data.

Output:
House 1's name is name1
House 7's name is name7


All times are GMT +5.5. The time now is 14:39.