Don't Know Why My Class Is Not Working

I am using Ubuntu; my compiler is Code::Blocks.
The instructions at the bottom are the exercises for one of the lessons in http://newdata.box.sk/bx/c/htm/ch06.htm#Heading2, from the book "Teach Yourself C++ in 21 Days". I have no idea why this won't compile. It sends an error message at 'int setage'.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
using namespace std;

class employee
{
    public:
    int setAge(int itsAge);
    int setyearsOfService(int itsyearsOfService);
    int setsalary(int itssalary);
    void getAge() const;
    void getyearsOfService() const;
    void getsalary() const;

    private:
    int age;
    int yearsOfService;
    int salary;
}

/*Functions declared between here and the next line of comments.*/
void employee::setAge(itsAge) {itsAge = age;}
void employee::setyearsOfService(itsyearsOfService){itsyearsOfService = yearsOfService;}
void employee::setsalary(itssalary)
{
    itssalary = salary;
}
int employee::getAge()
{
    return age;
}
int employee::getyearsOfService()
{
    return yearsOfService;
}
int employee::getsalary()
{
return salary;
}
/*Functions declared between here and the previous line of comments.*/

int main()
{
    employee Carl;
    Carl.setage(35);
    Carl.setyearsOfService(2);
    Carl.setsalary(30000);
    Carl.getAge();
    Carl.getyearsOfService();
    Carl.getsalary();

    employee Cindy;
    Cindy.setage(33);
    Cindy.setyearsOfService(1);
    Cindy.setsalary(29000);
    Cindy.getAge();
    Cindy.getyearsOfService();
    Cindy.getsalary();

    return 0;
}

/*
1. Write the code that declares a class called Employee with these data members: age, yearsOfService, and Salary.

2. Rewrite the Employee class to make the data members private, and provide public accessor methods to get and set each of the data members.

3. Write a program with the Employee class that makes two Employees; sets their age, YearsOfService, and Salary; and prints their values.

4. Continuing from Exercise 3, provide a method of Employee that reports how many thousands of dollars the employee earns, rounded to the nearest 1,000.

5. Change the Employee class so that you can initialize age, YearsOfService, and Salary when you create the employee.
*/
Last edited on
Lines 31/39 don't create classes, they instead prototype functions that return employees. Just remove the () and you should be fine.
I tried that, but the error is still at 'int setage'. If that's wrong, it means all my functions are screwed up somehow. :/ Thanks, though.
It looks like your formal paramaters of your class functions don't have any types associated with them.
closed account (zb0S216C)
noahpocalypse wrote:
void employee::setAge(itsAge) {itsAge = age; (sic)

Of what type is itsAge? Simply declaring a new symbol without a type-specifier is illegal. You didn't happen to come from a scripting background, did you? Just asking :)

Wazzak
Last edited on
Do not use that book. Aside from the fact that it isn't free and is likely published illegally on that site, it's one of the worst C++ books that exist (if it even deserves the description "C++ book" - what it teaches is not valid C++).

Use this from now on:
http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html
Don't skip the parts you think you already know. A lot of what you learned is outdated or just plain wrong.

Or a "real" book:
http://www.amazon.com/Primer-4th-Stanley-B-Lippman/dp/0201721481
Last edited on
@Framework/Wazzak
No I didn't. This is my first programming experience, other than a failed attempt at Visual Basic a few years ago. I think I can blame that on Microsoft's awful tutorials though.

@Athar
Alrighty; I'll give that a try.

I updated the code and added integers to the function declaration within 'employee'. Now the error is where I try to define all the accessor functions.
Without regard to any other possible errors, line 18 needs a semi-colon following the class definition.
Topic archived. No new replies allowed.