c++ - Undefined variables within header files -
thanks taking time view question. i've spent lot of time looking can't find solution problem:
i have class person its' respective header file person.h. cpp class having hard time understanding variables name, age, , iq are.
//person.cpp /* makes person. person has name, age, , iq level. create many of these objects within other classes. */ #include "person.h" person::person() //empty constructor { } person::person(string thename, int theage, int theiq) { name = thename; age = theage; iq = theiq; } string getname() { return name; //error: identifier "name" undefined } int getage() { return age; //error: identifier "age" undefined } int getiq() { return iq; //error: identifier "iq" undefined } void setname(string aname) { name = aname; //error: identifier "name" undefined } void setage(int aage) { age = aage; //error: identifier "age" undefined } void setiq(int aiq) { iq = aiq; //error: identifier "iq" undefined } the header file
//================================= // include guard #ifndef __person_h_included__ #define __person_h_included__ //================================= // forward declared dependencies //================================= // included dependencies #include <string> //for name //================================= using namespace std; class person { public: person(); //default constructor person(string thename, int theage, int theiq); //constructor string getname(); int getage(); int getiq(); void setname(int aname); void setage(int aage); void setiq(int aiq); private: string name; int age, iq; }; #endif //__person_h_ surely don't have define variables inside person.cpp right? knows variables name, age, , iq because saw header file already. why can not understand variables are?
if do, or if there i'm missing, make sure spell out me. i'm barely intermediate c++ programmer may not understand jargon. things scope, inheritance, , definitions go way on head.
thank time.
to ever part you're getting errors this: return type person::function name(parameters)
for declaration part of functions.
for example getage function this:
int person::getage() { return age; } the reason got errors because of following:
- first intention define functions declared in person class.
- in return statement wanted return age member of person class. returned age.
- the search age begins inside function body first. if not found searched in outer scope. if still not found age unknown identifier.
after fix happens this:
- the person:: part in return type person::function name(parameters) means in scope of person class.
- then in return statement return age member of person class. time we're looking inside scope of person class, hence return age member , error fixed.
Comments
Post a Comment