Class Fails at Runtime

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2009
Posts: 148
Reputation: Phil++ is an unknown quantity at this point 
Solved Threads: 3
Phil++ Phil++ is offline Offline
Junior Poster

Class Fails at Runtime

 
0
  #1
26 Days Ago
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'
  1. class Person
  2. {
  3. public:
  4. Person();
  5. Person(char theName[], float thePay, int theHours, bool Isworker);
  6. char* getName();
  7. float getPay();
  8. int getDepartment();
  9.  
  10. void setName(char theName[]);
  11. void setPay(float thePay);
  12. void setHours(int theHours);
  13. void setSalaried(bool Isworker);
  14.  
  15. private:
  16. char* name;
  17. float pay;
  18. int hours;
  19. bool worker;
  20. };

'Functions'
  1. #include <string>
  2. #include <iostream.h>
  3. #include "person.h"
  4.  
  5. Person::Person(){}
  6. Person::Person(char theName[], float thePay, int theHours, bool Isworker)
  7. {
  8. strcpy(name, theName);
  9. pay = thePay;
  10. hours = theHours;
  11. worker = worker;
  12. }
  13.  
  14. char* Person::getName()
  15. {
  16. return name;
  17. }
  18.  
  19. float Person::getPay()
  20. {
  21.  
  22. return pay;
  23. }
  24.  
  25. int Person::getHours()
  26. {
  27. return hours;
  28. }
  29.  
  30. void Person::setName(char theName[])
  31. {
  32. strcpy(name, theName);
  33. }
  34. void Person:: setPay(float thePay)
  35. {
  36. pay = thePayRate;
  37. }
  38. void Person::setHours(int theHours)
  39. {
  40. hours = theHoursWorked;
  41. }
  42. void Person::setSalaried(bool Isworker)
  43. {
  44. worker = Isworker;
  45. }

Any ideas? Thanks.
Last edited by Phil++; 26 Days Ago at 8:01 pm.
If you ask me questions through Private messaging I won't reply.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 359
Reputation: jonsca is an unknown quantity at this point 
Solved Threads: 43
jonsca jonsca is offline Offline
Posting Whiz
 
0
  #2
26 Days Ago
  1.  
  2. void Person:: setPay(float thePay)
  3. {
  4. pay = thePayRate;
  5. }
  6. void Person::setHours(int theHours)
  7. {
  8. hours = theHoursWorked;
  9. }
Have a problem here with those variable names. Also, does your compiler warn you about iostream.h? Should just be #include <iostream>
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 630
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster
 
0
  #3
26 Days Ago
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 48
Reputation: pecet is an unknown quantity at this point 
Solved Threads: 10
pecet pecet is offline Offline
Light Poster
 
0
  #4
26 Days Ago
what's more You try to copy string here:
  1. Person::Person(char theName[], float thePay, int theHours, bool Isworker)
  2. {
  3. strcpy(name, theName);
  4. //...
  5. }
but no memory is allocated for char* name.

IMO you should consider using std::string.
Last edited by pecet; 26 Days Ago at 5:10 am.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC