Thread: classes problem

  1. #1
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052

    Thumbs up classes problem

    I have written this program to test out my newly gained knowledge on classes, but my compiler (Dev-C++... soon getting MSVC++) says that I havn't declared to variables inside the class. To help you understand my problem, here are the errors....

    Code:
    c:\mydocu~1\people.cpp: In function `void outPutCiv()':
    c:\mydocu~1\people.cpp:38: `name' undeclared (first use this function)
    c:\mydocu~1\people.cpp:38: (Each undeclared identifier is reported only once
    c:\mydocu~1\people.cpp:38: for each function it appears in.)
    c:\mydocu~1\people.cpp:40: `creditCard' undeclared (first use this function)
    c:\mydocu~1\people.cpp:42: `extra' undeclared (first use this function)
    ...and here is the actual code....
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <string.h>
    #include <fstream.h>
    
    class civillian
    {
      public:
      char name[128];
      int creditCard;
      char extra[128];
    };
    
    class government
    {
      public:
      char name[128];
      int creditCard;
      char extra[128];
    };
    
    class military
    {
      public:
      char name[128];
      int creditCard;
      char extra[128];
    };
    
    void outPutCiv()
    {
      ofstream output;
    
      output.open("data.txt");
    
      output << "            Data for a Civillian\n\n";
      output << "Name: ";
      output << name.person;
      output << "\nCredit Card number: ";
      output << creditCard.person;
      output << "\nExtra information: ";
      output << extra.person;
      output.close();
    }
    
    void outPutGov()
    {
      ofstream output;
    
      output.open("data.txt");
    
      output << "            Data for a Government person\n\n";
      output << "Name: ";
      output << name.person1;
      output << "\nCredit Card number: ";
      output << creditCard.person1;
      output << "\nExtra information: ";
      output << extra.person1;
      output.close();
    }
    
    void outPutMil()
    {
      ofstream output;
    
      output.open("data.txt");
    
      output << "            Data for a Military person\n\n";
      output << "Name: ";
      output << name.person2;
      output << "\nCredit Card number: ";
      output << creditCard.person2;
      output << "\nExtra information: ";
      output << extra.person2;
      output.close();
    }
    
    void civInfo()
    {
      cout << "Enter the information for a civillian:\n";
      cout << "Enter your name: ";
      cin.getline(name.person, 128);
      cout << "Enter your credit card number: ";
      cin >> creditCard.person;
      cout << "Enter any extra information about this person: ";
      cin.getline(extra.person, 128);
      outPutCiv();
    }
    
    void govInfo()
    {
      cout << "Enter the information for a Government person:\n";
      cout << "Enter your name: ";
      cin.getline(name.person1, 128);
      cout << "Enter your credit card number: ";
      cin >> creditCard.person1;
      cout << "Enter any extra information about this person: ";
      cin.getline(extra.person1, 128);
      outPutGov();
    }
    
    void milInfo()
    {
      cout << "Enter the information for a Military person:\n";
      cout << "Enter your name: ";
      cin.getline(name.person2, 128);
      cout << "Enter your credit card number: ";
      cin >> creditCard.person2;
      cout << "Enter any extra information about this person: ";
      cin.getline(extra.person2, 128);
      outPutMil();
    }
    
    int main()
    {
      civillian person;
      government person1;
      military person2;
    
      cout << "Welcome to Chris' person-data application\n";
      cout << "Are you entering the data for a:\n";
      cout << "[1]Civillian\n[2]Government person\n[3]Military person\n";
      int choice = 0;
      cin >> choice;
      switch (choice)
      {
        case 1:
        system("cls");
        civInfo();
        break;
    
        case 2:
        system("cls");
        govInfo();
        break;
    
        case 3:
        system("cls");
        milInfo();
    
        default:
        cout << "You must enter a number between 1 and 3";
    }
      return 0;
    }
    Hope somebody can solve this mind buggling mystery

    Thanks
    -Chris

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    I didn't examine it closely, but
    name.person
    should be
    person.name
    You declare the object person of class civillian, so the class data a/o functions are on the right of the dot. Same with creditcard.person, etc.
    I don't know if it's required, but you might want to include a default constructor as well.

  3. #3
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    i have done what u said, but I still get the same errors. How do you include a constructor and what does it do? Would it fix my problem?

  4. #4
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    does anybody know ?

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    25
    First of all your BIGGEST mistake is by not defining a default constructor. This can and will lead to hard to find problems. Also you could make a class Person and derive the others classes from it. Anytime you have classes with similarities such as this it is a good practice to use inheritance. Will check your code in a second but this is what was really standing out.

  6. #6
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    You need to pass the object to the functions in order to access the data members. For example:

    civInfo();

    should be:

    civinfo(person1,person2);
    And to access the data members, for example would look like:

    person1.CreditCard = 23432;


    The main falt is that you didn't pass the objects that were instantiated in main()

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    25
    OK I just ran your code and seem some room for inprovement.
    1.)make your member variables protected. This gives you control over what is allowed to changeyour data and how.

    2.)use your classes to their ful extent. Like I stated before use inheritance to simplify your code.

    3.)the output function in main should be a member function of class Person(see note 2 above). By using the this pointer your class will take care of it's own input and output.

    4.)the info functions is an excellent canidate for a member function. Same reason as above.

    5.)Your function calls to info are all wrong(no offense). The proper way would be to(if not made a member function of person) pass the function the address of a Person and let it do the operations from there.

    void Info(Person* pMyPerson);//Function declaration

    void Info(Person& pMyPerson)
    {
    float temp;
    char Chartemp[30];
    pMyPerson->GetInfo(); //Function that would call a cout<<
    //and prompt for the name.
    cout << "Enter your name: ";
    cin.getline(CharTemp, 128);
    pMyPerson->SetName(CharTemp);
    cout << "Enter your credit card number: ";
    cin >> temp;
    pMyPerson->SetCreditCard(temp);
    cout << "Enter any extra information about this person: ";
    cin.getline(CharTemp, 128);
    pMyPerson->SetExtraInfo(CharTemp);
    outPutCiv();
    }

  8. #8
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    ok, i have completely fixed my code exept that i keep getting these errors (different to the other ones)
    Code:
    c:\mydocu~1\chris\people.cpp: In method `void civillian::civInfo()':
    c:\mydocu~1\chris\people.cpp:19: `person' undeclared (first use this function)
    c:\mydocu~1\chris\people.cpp:19: (Each undeclared identifier is reported only once
    c:\mydocu~1\chris\people.cpp:19: for each function it appears in.)
    c:\mydocu~1\chris\people.cpp: In method `void government::govInfo()':
    c:\mydocu~1\chris\people.cpp:53: `person1' undeclared (first use this function)
    c:\mydocu~1\chris\people.cpp: In method `void military::milInfo()':
    c:\mydocu~1\chris\people.cpp:87: `person2' undeclared (first use this function)
    and here is my code
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <string.h>
    #include <fstream.h>
    
    class civillian
    {
      public:
      char name[128];
      int creditCard;
      char extra[128];
    
      void civillian::civInfo()
    {
    
    
      cout << "Enter the information for a civillian:\n";
      cout << "Enter your name: ";
      cin.getline(person.name, 128);
      cout << "Enter your credit card number: ";
      cin >> person.creditCard;
      cout << "Enter any extra information about this person: ";
      cin.getline(person.extra, 128);
    
      ofstream output;
    
      output.open("data.txt");
    
      output << "            Data for a Civillian\n\n";
      output << "Name: ";
      output << person.name;
      output << "\nCredit Card number: ";
      output << person.creditCard;
      output << "\nExtra information: ";
      output << person.extra;
      output.close();
    }
    };
    
    class government
    {
      public:
      char name[128];
      int creditCard;
      char extra[128];
    
      void government::govInfo()
    {
    
    
      cout << "Enter the information for a Government person:\n";
      cout << "Enter your name: ";
      cin.getline(person1.name, 128);
      cout << "Enter your credit card number: ";
      cin >> person1.creditCard;
      cout << "Enter any extra information about this person: ";
      cin.getline(person1.extra, 128);
    
      ofstream output;
    
      output.open("data.txt");
    
      output << "            Data for a Government person\n\n";
      output << "Name: ";
      output << person1.name;
      output << "\nCredit Card number: ";
      output << person1.creditCard;
      output << "\nExtra information: ";
      output << person1.extra;
      output.close();
    }
    };
    
    class military
    {
      public:
      char name[128];
      int creditCard;
      char extra[128];
    
      void military::milInfo()
    {
    
    
      cout << "Enter the information for a Military person:\n";
      cout << "Enter your name: ";
      cin.getline(person2.name, 128);
      cout << "Enter your credit card number: ";
      cin >> person2.creditCard;
      cout << "Enter any extra information about this person: ";
      cin.getline(person2.extra, 128);
    
      ofstream output;
    
      output.open("data.txt");
    
      output << "            Data for a Military person\n\n";
      output << "Name: ";
      output << person2.name;
      output << "\nCredit Card number: ";
      output << person2.creditCard;
      output << "\nExtra information: ";
      output << person2.extra;
      output.close();
    }
    };
    
    int main()
    {
      cout << "Welcome to Chris' person-data application\n";
      cout << "Are you entering the data for a:\n";
      cout << "[1]Civillian\n[2]Government person\n[3]Military person\n";
      int choice = 0;
      cin >> choice;
      switch (choice)
      {
        case 1:
        system("cls");
        civillian person;
        person.civInfo();
        break;
    
        case 2:
        system("cls");
        government person1;
        person1.govInfo();
        break;
    
        case 3:
        system("cls");
        military person2;
        person2.milInfo();
    
        default:
        cout << "You must enter a number between 1 and 3";
    }
      return 0;
    }

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    23
    If you lookl at your code, you can see that person, person1 and person2 are defined in the funcion main(), this means that the methods civInfo(), govInfo() & milInfo() knows nothing about them.
    But, since they are methods of an object, they can simply access its memebers, so in these methods, you should simply use "name" instead of "person.name". That's the idea of OO programming.

    two more notes:
    1. you might want to derive those three classes from one common class that holds common data members. This base class would then have a virtual Info)( method, that would be implemented differently by each of the derived classes, and then main() function would call the same function for different objects of different classes.
    2. in the switch statement in the main function, since you declare variables in the various cases, you might want to enclose the entire block after each case with braces - {} - to avoid compiler errors and warnings.

  10. #10
    Registered User
    Join Date
    Aug 2001
    Posts
    72
    ok here is an implementation using common base class and virtual information
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <string.h>
    #include <fstream.h>
    
    // base class
    class CPerson {
    protected:
    	// members
    	char name[128];
    	int creditCard;
    	char extra[128];
    public:
    	// read data from console
    	void ReadInfo()
    	{
    		cout << "Enter the information for a "<< strName() <<":\n";
    		cout << "Enter your name: ";
    		cin.getline(name, 128);
    		cout << "Enter your credit card number: ";
    		cin >> creditCard;
    		cin.ignore();
    		cout << "Enter any extra information about this person: ";
    		cin.getline(extra, 128);
    	}
    	
    	// write to the output stream
    	void WriteInfo(ostream& output) const
    	{
    		output << "            Data for a "<<strName()<<"\n\n";
    		output << "Name: ";
    		output << name;
    		output << "\nCredit Card number: ";
    		output << creditCard;
    		output << "\nExtra information: ";
    		output << extra;	
    	}
    
    	// virtual class info. Note the first line of each methods above
    	virtual const char* strName() const = 0;
    };
    
    // here are the descendants of the base class
    class civillian : public CPerson {
    public:
    	const char* strName() const
    	{
    		return "Civillian";
    	}
    };
    
    class government : public CPerson {
    public:
    	virtual const char* strName() const 
    	{
    		return "Government";
    	}
    };
    
    class military : public CPerson {
    public:
    	virtual const char* strName() const 
    	{
    		return "Military";
    	}
    };
    
    int main()
    {
    	cout << "Welcome to Chris' person-data application\n";
    	cout << "Are you entering the data for a:\n";
    	cout << "[1]Civillian\n[2]Government person\n[3]Military person\n";
    	int choice = 0;
    	cin >> choice;
    	// to skip the <crlf> left in the input buffer
    	cin.ignore();
    	// a base class pointer
    	CPerson* pPerson = NULL;
    	// create a specific descendant of the base class given the input
    	switch (choice)
    	{
    		case 1:
    			pPerson = new civillian;
    		break;
    		case 2:
    			pPerson = new government;
    		break;
    		case 3:
    			pPerson = new military;
    		break;
    		default:
    			cout << "You must enter a number between 1 and 3";
    		break;
    	}
    	if (pPerson)
    	{
    		system("cls");
    		// read it from console
    		pPerson->ReadInfo();
    
    		// write it to the output stream
    		ofstream output;
    		output.open("data.txt");
    		pPerson->WriteInfo(output);
    		output.close();
    	}
    	return 0;
    }

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    23
    seems fine, except that you forgot to delete pPerson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with classes and pointers
    By Akkernight in forum C++ Programming
    Replies: 18
    Last Post: 02-21-2009, 06:21 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Having a problem with Classes
    By FoxTrot in forum C++ Programming
    Replies: 10
    Last Post: 09-06-2007, 07:40 PM
  4. Problem with destructors.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2004, 12:30 PM
  5. problem w/ nested templatized classes
    By *ClownPimp* in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2002, 07:58 AM