Thread: Probably a simple question

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    1

    Probably a simple question

    I am new and have been working my way through the tutorials and am now officially annoyed. I have started experimenting with classes and seeing how I can work them into a linked list style of operation. My code is here:

    Code:
     
    class Database
    {
     public:
       // public variables
       char employee_name [50];  // employee name
       int id_number;               // identification number
       int group_number;            // group number
     
       Database ();                 // constructor definition
       ~Database ();                // destructor definition
          
       Database *next;              // pointer to the next entry
    
     // function to add a new employee
      void add_new_user();
    
     protected:
       char supervisor [50];        // name of supervisor
       int ranking;                 // ranking within group
       float salary;                // employee's salary
       
       void getsupervisor (char *super, int group);
       // determine the employee rank
       int getrank (int id, int group);    
       // determine the salary
       float getsalary (int rank, int group);
       
    };
    
    void Database::add_new_user( )
    {
          char name [50];
          int group;
          int id;
          cout << "Enter the employee's name ";
          cin.getline (name, 50, '\n');
          cout << "Enter the employee's group ";
          cin >> group;
          cin.ignore();                 // ignore enter
          cout << "Enter the employee's ID number ";
          cin >> id;
          cin.ignore();
          
          strncpy(employee_name, name, 50);
          group_number = group;
          id_number = id;
          ranking = getrank(id, group);
          salary = getsalary(ranking, group);
          getsupervisor(supervisor, group);
    
    }    
    
    int main ()
    {
        Database *root;     // delcare a pointer to a root database
        Database *employee;  // declare a pointer to the employee database
        employee = root;                    // set the employee                             
        
        char entry;                         // entry selection
    
        while (entry != 'e')
        {
          cout << "(a)dd new employee \n";
          cout << "(p)rint all employees \n";
          cout << "(e)xit \n";
          cin >> entry;
          
          // switch statement for entry
          switch (entry)
          {
             case 'a' :
               employee -> next = new Database; // create a new database
               employee = employee -> next;     // set pointer to new employee
               employee.add_new_user();
               break;
             case 'p' :
               employee = root;
               while (employee != NULL)
               {
                 cout << "Name: " << employee -> employee_name << "\n";
                 cout << "Group: " << employee -> group_number << "\n";
                 cout << "ID number " << employee -> id_number << "\n";
     
                 employee = employee -> next;
               }
               cout << "Name: " << employee -> employee_name << endl;
               cout << "Group: " << employee -> group_number << endl;
               cout << "ID number " << employee -> id_number << endl;
               break;
            case 'e' :
               cout << "Ending" << endl;
               break;
          }
        }   
              
        cin.get();
    }
    When I get to employee.add_new_user, I get this error:

    "141 C:\Documents and Settings\Owner\My Documents\dbase_exp.cpp `add_new_user' has not been declared" and I can't understand why. Could somebody enlighten me?

    Thanks

    Chris

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    #include <iostream>
    
    using namespace std;
    
    ...
    
    employee->add_new_user();

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
        Database *root;     // delcare a pointer to a root database
        Database *employee;  // declare a pointer to the employee database
        employee = root;
    This is quite analogous to simpler code:
    Code:
        int a, b; 
        a = b;
    a and b would be equal but still containing a garbage value. This is the same that happens in your code, you have invalid pointers that don't point to anything meaningful.

    A very serious design problem is that you are reinventing a linked list, and exposing the mechanism of it to the users of the class (for example, the next pointer which is the "easiest-to-mess-up" thing in this code and should be encapsulated most carefully, is made public!). You are leaving the hardest stuff - managing the linked list - up to the user of Database.

    If you are just learning classes, may-be you ought to start with something easier (e.g forget about your own linked list, and use std::list) and concentrate on good practices of class design (e.g data hiding) instead.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM