Declaring an array of objects of a class

This is a discussion on Declaring an array of objects of a class within the C++ Programming forums, part of the General Programming Boards category; Our C++ professor assigned a group project that requires us to create an addressbook style program. The program must ask ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Location
    Los Gatos, California, United States
    Posts
    19

    Declaring an array of objects of a class

    Our C++ professor assigned a group project that requires us to create an addressbook style program. The program must ask the user to input a person's contact information (ie. Name, address, email, phone#) and store them in a list that can be sorted, searched, and printed to the console. We have everything working, except for the part where we store more than one contact. We have two classes, one in which the user inputs the data (class Person), and another that saves, searches, and sorts that data (class List). At this point in our development, our logic says that each new person entered into the addressbook will be stored as an Object of one of our classes. We would like to accomplish this by declaring an array of objects of class person inside class list. We are receiving a compiler error relating to the data type of our array. Any Ideas?

    Code:
    //Module Revied Nov 21, 2011: NDA
    #ifndef LIST_H
    #define LIST_H
    #include <string>
    #include "Person.h"
    #include "List.h"
    using namespace std;
    
    
    /* An object of this class will be an array which stores a list of contacts that the user has entered. The entire list will be saved to disk. This class will define functions to search the list and print it to the console.*/	
    
    
    	//The number of constants stored is 100 for now. can be changed.
    const int MAX_PEOPLE = 100;
    
    
    class List
    {
    private:
    
    
    	int numContacts;
    	int nextEmptySpot();
    	Person contactArray[MAX_PEOPLE]; //Line returns 3 errors
    
    
    public:
    //code continues, deleted to shorten. all other code produces no //errors
    We cannot seem to make the computer recognize Person as a valid data type, even thought it is the name of our other class, and we have included it's header file. Any ideas on how to fix this array?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,936
    You defined MAX_PEOPLE in the header. This is problematic because then when you include the header in different source files, you break the one definition rule. Since MAX_PEOPLE is specific to List, you might do this instead:
    Code:
    class List
    {
    private:
        static const int MAX_PEOPLE = 100;
        // ...
        Person contactArray[MAX_PEOPLE];
        // ...
    So, you declare MAX_PEOPLE as a static const member. Normally, this means that you will define it in the source file, but as a special case for static const members of integer types, you can define and initialise it inline in the class definition.

    Next, you should remove the using directive (using namespace std; ). Using directives and using declarations should not be used in header files except within a restricted scope.

    Quote Originally Posted by RRTT
    We are receiving a compiler error relating to the data type of our array. Any Ideas?
    What is the compile error?

    Looking at your code, one thing to check is whether the class definition of the Person class ends with a semi-colon. Another thing: is this file supposed to be List.h? If so, why do you #include "List.h"?
    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

  3. #3
    Registered User
    Join Date
    Oct 2011
    Location
    Los Gatos, California, United States
    Posts
    19
    Updated List.h to:
    Code:
    //Module Revied Nov 21, 2011: NDA
    #ifndef LIST_H
    #define LIST_H
    #include <string>
    #include "Person.h"
    
    
    
    
    /* An object of this class will be an array which stores a list of contacts that the user has entered. The entire list will be saved to disk. This class will define functions to search the list and print it to the console.*/	
    
    
    	//The number of constants stored is 100 for now. can be changed.
    
    
    
    
    class List
    {
    private:
    	static const int MAX_PEOPLE = 100;
    	int numContacts;
    	int nextEmptySpot();
    	Person contactArray[MAX_PEOPLE]; //Line returns 3 errors
    
    
    public:
    //code ended to save space in forum, it continues with function prototypes
    This code still happens to return the following 3 compiler errors

    error C2146: syntax error : missing ';' before identifier 'contactArray'\
    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    (I get the same error twice)

    As I understand them, these errors are produced because the computer does not recognize class Person as a valid data type.

    I double checked, and class Person does infact end in a semicolon.

    Thanks a bunch for your help on this. It's been a day full of debugging this lol.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,936
    Quote Originally Posted by RRTT
    I double checked, and class Person does infact end in a semicolon.
    I tested with this code, and my compiler did not report an error:
    Code:
    #ifndef LIST_H
    #define LIST_H
    #include <string>
    
    class Person {};
    
    class List
    {
    private:
        static const int MAX_PEOPLE = 100;
        int numContacts;
        int nextEmptySpot();
        Person contactArray[MAX_PEOPLE];
    };
    
    #endif
    
    int main()
    {
        List x;
    }
    Clearly, the problem lies with code that you did not show.
    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

  5. #5
    Registered User
    Join Date
    Oct 2011
    Location
    Los Gatos, California, United States
    Posts
    19
    Now that I think about it, it is likely the issue resides with code in the function definition that calls the array. Since this wasn't the line that the compiler referred to as the error, i figured that it was correct. This is almost certainly what is going wrong, check the function void List::setNewContact() for me please.
    Code:
    #include "List.h"
    #include "Person.h"
    #include <string>
    #include <iostream>
    using namespace std;
    
    
    /*This file contains function definitions for class List*/
    
    
    
    
    //prints the entire contact list
    void List::showContactList()
    {
    	cout << "showContactList called" << endl;
    }
    
    
    //returns the integer value of then next empty spot in the contactList array. under construction.
    int List::nextEmptySpot()
    {
    	numContacts += 1;
    	return numContacts;
    }
    
    
    //puts the new person object into the array
    void List::setNewContact()
    {
    List addressObject;
    Person putContact;
    contactArray[addressObject.nextEmptySpot()] = putContact;
    }
    
    
    void List::saveList()
    // This function will contact list to disk
    {
    	cout << "saveList called" << endl;
    }
    
    
    void List::searchByLastName()
    //This function will search the database for contacts by last name
    {
    	cout << "SearchByLastName called" << endl;
    }
    
    
    void List::searchByEmail()
    
    
    //this function will search the database for contacts by email
    {
    	cout << "SearchByEmail called" << endl;
    }
    
    
    void List::searchByZipCode()
    //this function will search the database for contacts in a given ZIP code
    {
    	cout<< "SearchByZipCode called" <<endl;
    }
    
    
    void List::searchByPhoneNumber()
    //this function will search the database for contacts by telephone number
    {
    	cout << "SearchByPhoneNumber called" << endl;
    }
    This program is clearly far from complete, many of the functions just print a statement confirming that they were successfully called.

    Here is the full code of the file List.h

    Code:
    #ifndef LIST_H
    #define LIST_H
    #include <string>
    #include "Person.h"
    
    
    
    
    /* An object of this class will be an array which stores a list of contacts that the user has entered. The entire list will be saved to disk. This class will define functions to search the list and print it to the console.*/	
    
    
    	//The number of constants stored is 100 for now. can be changed.
    
    
    
    
    class List
    {
    private:
    	static const int MAX_PEOPLE = 100;
    	int numContacts;
    	int nextEmptySpot();
    	Person contactArray[MAX_PEOPLE]; //Line returns 3 errors
    
    
    public:
    	
    	List();
    	
           	//prints the entire contact list
           	void showContactList();
    
    
    	//puts the new person object into the array
    		void setNewContact();
    
    
          	//saves the contact list to disk
    void saveList();
    
    
    	//This function will search the database for contacts by last name
    void searchByLastName();
    
    
    //this function will search the database for contacts by email
    void searchByEmail();
    
    
    //this function will search the database for contacts in a given ZIP code
    void searchByZipCode();
    
    
    //this function will search the database for contacts by telephone number
    
    
    void searchByPhoneNumber();
    };
    
    
    #endif
    I really appreciate your help with this issue.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,936
    Show Person.h
    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

  7. #7
    Registered User
    Join Date
    Oct 2011
    Location
    Los Gatos, California, United States
    Posts
    19
    Person.h
    Code:
    #ifndef PERSON_H
    #define PERSON_H
    #include <string>
    #include "List.h"
    using namespace std;
    
    
    /*An object of class Person will contain the contact info that the user inputs for one contact and what the user expects to be stored in their list of contacts. When an object of class Person is created, the user’s input for contact’s first name, last name, email, address, zip-code, phone number, and notes are entered into an array*/
    
    
    class Person
    {
       	private:
    		string firstName;
    		string lastName;
    		string email;
    		string address;
    		string zip;
    		string phone;
    		string notes;
                
    public:
        	//Constructor
           	Person();
    
    
           	//This function prompts the user to input the contact’s first name, last name,
        	//email, address, zip-code, phone number, and notes.
           	void enterNewContact();
    };
    
    
    #endif
    I'm if the rules here regulate how much code you can ask for help on, but I don't want to abuse this resource, so let me know if I get too pushy. I really appreciate your help, again.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,936
    Oh, I understand now. In Person.h, look carefully at:
    Code:
    #include "List.h"
    So, Person.h includes List.h which includes Person.h. Circular inclusion is Bad. You should remove that line from Person.h
    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

  9. #9
    Registered User
    Join Date
    Oct 2011
    Location
    Los Gatos, California, United States
    Posts
    19
    This was an incredible help. Thanks so much for your time and knowledge.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,789
    Quote Originally Posted by laserlight View Post
    You defined MAX_PEOPLE in the header. This is problematic because then when you include the header in different source files, you break the one definition rule.
    Seeing as this is C++, the compiler should treat MAX_PEOPLE as a constant expression, though, shouldn't it? This would avoid multiple definition errors.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic array of objects in class
    By bejfake in forum C++ Programming
    Replies: 4
    Last Post: 10-25-2010, 08:20 AM
  2. Replies: 3
    Last Post: 07-15-2010, 07:53 PM
  3. Array of class objects
    By te5la in forum C++ Programming
    Replies: 4
    Last Post: 07-23-2008, 05:37 PM
  4. Array of class objects
    By GCNDoug in forum C++ Programming
    Replies: 29
    Last Post: 03-26-2008, 04:30 PM
  5. Replies: 4
    Last Post: 10-16-2003, 11:26 AM

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