| | |
Class Fails at Runtime
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2009
Posts: 148
Reputation:
Solved Threads: 3
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'
'Functions'
Any ideas? Thanks.
'Class'
C++ Syntax (Toggle Plain Text)
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'
C++ Syntax (Toggle Plain Text)
#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.
Last edited by Phil++; 26 Days Ago at 8:01 pm.
If you ask me questions through Private messaging I won't reply.
•
•
Join Date: Sep 2009
Posts: 359
Reputation:
Solved Threads: 43
0
#2 26 Days Ago
C++ Syntax (Toggle Plain Text)
void Person:: setPay(float thePay) { pay = thePayRate; } void Person::setHours(int theHours) { hours = theHoursWorked; }
#include <iostream> •
•
Join Date: Feb 2008
Posts: 630
Reputation:
Solved Threads: 46
0
#3 26 Days Ago
First of all, you should
Second, you have to
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
#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
•
•
Join Date: Oct 2009
Posts: 48
Reputation:
Solved Threads: 10
0
#4 26 Days Ago
what's more You try to copy string here:
but no memory is allocated for char* name.
IMO you should consider using std::string.
C++ Syntax (Toggle Plain Text)
Person::Person(char theName[], float thePay, int theHours, bool Isworker) { strcpy(name, theName); //... }
IMO you should consider using std::string.
Last edited by pecet; 26 Days Ago at 5:10 am.
![]() |
Similar Threads
- Dev-C++ - [Build Error] [main.o] Error 1 (C++)
- Class dependency injection (C#)
- file IO and fuctions (C++)
- Runtime.getruntime.exec(); for my java compiler (Java)
- Really need help with the interactive Calculator program, or will be dead (C++)
- c++ (C++)
- Any Java Source for 16 Questions (Java)
- WIN32 GUI application - Problem popping up dialog box (C++)
- Combo Box (C++)
- Error while executing (Java)
Other Threads in the C++ Forum
- Previous Thread: does anyone know to do program like peteranswers??
- Next Thread: Finding the highest and the lowest number out of 5 inputs...
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





