| | |
Class Fails at Runtime
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2009
Posts: 151
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++; Nov 5th, 2009 at 8:01 pm.
If you ask me questions through Private messaging I won't reply.
•
•
Join Date: Sep 2009
Posts: 576
Reputation:
Solved Threads: 77
0
#2 Nov 5th, 2009
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: 638
Reputation:
Solved Threads: 46
0
#3 Nov 5th, 2009
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: 59
Reputation:
Solved Threads: 11
0
#4 Nov 6th, 2009
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; Nov 6th, 2009 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 |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





