Hey, I've created this class, it compiles OK but when I go to use the class it fails. It comes up with a pop-up saying something like 'Unexpected Error'

'Class'

class Person
{
   public:
          Person();
          Person(char theName[], float thePay, int theHours, bool Isworker);
          char* getName();
          float getPay();
          int getDepartment();
          
          void setName(char theName[]);
          void setPay(float thePay);
          void setHours(int theHours);
          void setSalaried(bool Isworker);
          
   private:
           char* name;
           float pay;
           int hours;
           bool worker;
};

'Functions'

#include <string>
#include <iostream.h>
#include "person.h"

Person::Person(){}
Person::Person(char theName[], float thePay, int theHours, bool Isworker)
{
   strcpy(name, theName);
   pay = thePay;
   hours = theHours;
   worker = worker;                      
}

char* Person::getName()
{
   return name;
}

float Person::getPay()
{
      
   return pay;     
}

int Person::getHours()
{
   return hours;     
}

void Person::setName(char theName[])
{
     strcpy(name, theName);
}
void Person:: setPay(float thePay)
{
     pay = thePayRate;
}
void Person::setHours(int theHours)
{
     hours = theHoursWorked;
}
void Person::setSalaried(bool Isworker)
{
   worker = Isworker;    
}

Any ideas? Thanks.

Recommended Answers

All 3 Replies

void Person:: setPay(float thePay)
{
     pay = thePayRate;
}
void Person::setHours(int theHours)
{
     hours = theHoursWorked;
}

Have a problem here with those variable names. Also, does your compiler warn you about iostream.h? Should just be #include <iostream>

First of all, you should #include <iostream> instead of #include <iostream.h> . This is the "new" c++ way to do it.

Second, you have to #include <cstring> to use strcpy.

Third, you have not declared getHours in the header file. To show you this, the compiler says: "error: no 'int Person::getHours()' member function declared in class 'Person'".

Fix these things and let us know if you have any more problems.

David

what's more You try to copy string here:

Person::Person(char theName[], float thePay, int theHours, bool Isworker)
{
strcpy(name, theName);
//...
}

but no memory is allocated for char* name.

IMO you should consider using std::string.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.