PDA

View Full Version : Help in C++


TJ9
10-01-01, 10:41 AM
I am trying to write a program for a C++ tutorial. Here is the program so far:

#include <iostream.h>


class Employee
{
public: // public accessors

int getAge() const;
void setAge(unsigned int pAge);

int getYears()const;
void setYears(unsigned int pYears);

int getSalary()const;
void setSalary(unsigned int pSalary);

private: // private member data
int pAge;
int pYearsOfServoce;
int pSalary;
};

int Employee ::getAge() //errors here?
{
return pAge;
}

int Employee::getYears()
{
return pYearsOfServoce;
}

int Employee::getSalary()
{
return pSalary;
}
int main()
{
Employee Tim;
Employee Bob;

Tim.setAge(25);
Tim.setYears(3);
Tim.setSalary(35000);

Bob.setAge(25);
Bob.setYears(1);
Bob.setSalary(24000);
cout << "Tim is " ;
cout << Tim.getAge() ;
cout <<"years old. \n" ;
cout << "Bob is " ;
cout << Bob.getAge() ;
cout <<"years old. \n" ;

return 0;
}

when i try to run it it givbes me the error:
Compiling...
chap6ques35.cpp
C:\Program Files\DevStudio\MyProjects\chap6ques35\chap6ques35 .cpp(24) : error C2511: 'getAge' : overloaded member function not found in 'Employee'
C:\Program Files\DevStudio\MyProjects\chap6ques35\chap6ques35 .cpp(29) : error C2511: 'getYears' : overloaded member function not found in 'Employee'
C:\Program Files\DevStudio\MyProjects\chap6ques35\chap6ques35 .cpp(34) : error C2511: 'getSalary' : overloaded member function not found in 'Employee'
Error executing cl.exe.

chap6ques35.obj - 3 error(s), 0 warning(s)


Can anyone help me?

TJ9
10-01-01, 10:53 AM
Okay now. I got the first problem finished by changing the lines:
public: // public accessors

int getAge()const ;
void setAge(unsigned int Age);

int getYears()const;
void setYears(unsigned int Years);

int getSalary()const;
void setSalary(unsigned int Salary);
to:
public: // public accessors

int getAge() ;
void setAge(unsigned int Age);

int getYears();
void setYears(unsigned int Years);

int getSalary();
void setSalary(unsigned int Salary);.

Now it compiles but when I try to link it is gives the following:
Linking...
chap6ques35.obj : error LNK2001: unresolved external symbol "public: void __thiscall Employee::setSalary(unsigned int)" (?setSalary@Employee@@QAEXI@Z)
chap6ques35.obj : error LNK2001: unresolved external symbol "public: void __thiscall Employee::setYears(unsigned int)" (?setYears@Employee@@QAEXI@Z)
chap6ques35.obj : error LNK2001: unresolved external symbol "public: void __thiscall Employee::setAge(unsigned int)" (?setAge@Employee@@QAEXI@Z)
Debug/chap6ques35.exe : fatal error LNK1120: 3 unresolved externals
Error executing link.exe.

chap6ques35.exe - 4 error(s), 0 warning(s)

Now I am really stuck.

engjohn
10-01-01, 02:29 PM
zip your .cpp source file and I will look at it.

Or you can email it to me at engjohn@hotmail.com

TJ9
10-03-01, 10:24 AM
I talked to some other people and this is what they told me to do.
The const should have been written like this:
int getAge()const ;

and
I needed to add

void Employee::setAge(unsigned int age)
{
pAge = age;
}
for all the set commands.

Thanks for trying.