Like Tree3Likes
  • 1 Post By laserlight
  • 2 Post By jimblumberg

Passing Array of Object gives compilation ERROR

This is a discussion on Passing Array of Object gives compilation ERROR within the C++ Programming forums, part of the General Programming Boards category; Hello Eveybody This is my question Define a class named HOUSING in C++ with the following descriptions: Private members REG_NO ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    1

    Passing Array of Object gives compilation ERROR

    Hello Eveybody


    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
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,655
    The prototypes in the declaration and definition of your function are different
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    Ugh. I have answered this elsewhere.
    Salem likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    Since no one has mentioned it, main shall return int, and you must never ever use std::cin >> to read into a char array. Use std::string!
    h is an uninitialized array of pointers, which is silly. You can't do stuff with uninitialized pointers. I suggest you go back to basics to understand how classes work, and how pointer work.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,564
    and you must never ever use std::cin >> to read into a char array.
    That "must never" is incorrect. It is possible to properly use the extraction operator>> to extract C-strings, provided you limit the number of characters you will read with a setw() prior to the extraction.
    Code:
    char name[10];
    cin >> setw(10) >> name;
    I do however agree that using std::string would be better. Also using getline() would be my preferred way of retrieving this information into a string or a C-string.

    Jim
    laserlight and iMalc like this.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    You are right. It is incorrect. But I'd rather have newbies believe it is correct than risk them misusing it.
    That is why I stated it must never be used. Better to lead newbies on a safe path and letting them find out the evil path by themselves when they're more experienced.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    2,564
    Better to lead newbies on a safe path and letting them find out the evil path by themselves when they're more experienced.
    And what is this evil path you talk about? There is nothing inherently evil about properly using the extraction operator, nor is there anything inherently evil about character strings. In my opinion not teaching the proper way of using these constructs is the true evil.

    Jim

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    Character strings are evil because they are prone to buffer overflows, reading-past-the-end and use of unnatural syntax to manipulate them.
    Use of the extraction operator is not evil per se, but it is evil to use in conjunction with a character array because it's prone to buffer overflows.
    IMO, a newbie should not need not know about this stuff, nor should they have to know how to use them safely. It is precisely because books and schools teach this crap that C++ is considered a difficult and low-level language.
    These are advanced topics that they should of when they are well versed in the language and in computer security.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    2,564
    In my opinion sticking ones head in the ground is much worse than teaching how to properly use these constructs. Dealing with character strings and pointers is problematic but these constructs can't be ignored, they are an integral part of the language.

    I do agree however that many C++ books and instructors wait far too long before they start teaching C++ concepts like std::string and the container classes.

    Jim

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    Were it that I thought that char arrays would be integral to newbies, I would agree, but I do not.
    I agree that if you are learning a construct, then you should learn its quirks, ups and downs.
    Stuff that are prone to buffer overflows are best left as advanced topics IMO.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why a[i] is not compiling . array compilation error
    By nkrao123@gmail. in forum C Programming
    Replies: 17
    Last Post: 09-06-2011, 08:49 AM
  2. compilation error on array assignment.
    By sanddune008 in forum C Programming
    Replies: 2
    Last Post: 07-26-2010, 02:50 AM
  3. Replies: 6
    Last Post: 04-04-2010, 11:48 AM
  4. Conditional compilation for object files in Makefile?
    By jutirain in forum C++ Programming
    Replies: 13
    Last Post: 12-19-2007, 05:23 AM
  5. Passing an array of a class object into a function
    By maoqiu in forum C++ Programming
    Replies: 3
    Last Post: 10-25-2007, 08:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21