Help Needed!: Classes/Methods

Hey guys. So I am currently learning C++ from a book called "SAMS Teach Yourself C++ in 24 Hours!" and I was following some instructions on classes and methods. But when I try to compile this, I keep getting an error: "'age' is not a type" and "'age' is not declared". Here is the code here. Would really appreciate it if someone would help me out :).

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
#include <iostream>
#include <string>

using namespace std;

class Cat {
	private:
		int age;
		
	public:
		int getAge();
		void setAge(age);
};

int Cat::getAge() {
	return age;
}

void Cat::setAge(age) {
	this -> age = age;
}

int main() {
	
	Cat fluffy;
	fluffy.setAge(10);
	cout << "Fluffy is " << fluffy.getAge() << " years old!" << endl;
	
}
The problem is with your setAge() method. You need to declare the type for the argument.

void Cat::setAge(int age);

I'd be generally wary of those '24 hours' type books.
"SAMS Teach Yourself C++ in 24 Hours!"

I havent heard of this book, but the title is so cringy that I can only assume there are much better books for one to read out there, like A tour of c++ by the creator of c++ -

http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list

You're getting the problem because age is not a type, and age is indeed not declared.

void setAge(age); // pass age as int. setAge(int age)

void Cat::setAge(age) // pass age as int. setAge(int age)
Last edited on
Topic archived. No new replies allowed.