problem with a class (newbie problem)

27 Sep 2012

Hi

I'm learning the c++ language with a book i've got (c++ in 24 hours from Jesse Liberty). But when testing the use of classes, i've got a problem compiling this code:

//main.cpp
#include "mbed.h"
#include "Cat.h"

typedef DigitalOut DO;

DO myled1(LED1);
DO myled2(LED2);
 
int main() { 
    cat Frisky(1);
    
    printf("START\n\r");
    printf("AGE 1:%i\n\r", Frisky.GetAge());
    Frisky.SetAge(5);
    printf("AGE 2:%i\n\r", Frisky.GetAge());
    printf("STOP\n\r");
    
    while(1) {
        myled1 = 1;
        wait(0.2);
        myled1 = 0;
        wait(0.2);
    }
}

//Cat.h
class Cat {
    public:
    Cat (int initialAge);
    int GetAge();
    void SetAge(int age);
    
    private:
    int itsAge;
);

//Cat.cpp
#include "Cat.h"

Cat::Cat(int initialAge) {
    itsAge = initialAge;
)

int Cat::GetAge() {
    return itsAge;
}

void Cat::SetAge(int age){
    itsAge = age;
}

Compiler give's me the error: Expected a declaration on Cat.h

What am I doing wrong?

27 Sep 2012

Your cat.h does not end with an '}' but with an ')'. Small typo :)

27 Sep 2012

You have to be very careful with getting brackets and capitalisation correct,

I noticed errors on the following lines: 11, 36, 43

27 Sep 2012

Crap, I searched 3 hours to figure out what's wrong with te code... :-) I'll triple check those brackets next time!

Fixed all the brackets and made the C uppercase. (on line 11) Works fine now.

Sorry for this stupid mistake and thanks for the help!

27 Sep 2012

Its no problem, you tend to read what you think should be there not what is there, its much easier to see problems in other peoples code then your own =)