Trouble with Private data Accessors

I'm trying to learn about handling private class data by using accessor methods. I wrote the following code in Visual C++ express as a test, but I keep getting the errors shown in the comments listed below, and I'm not sure how to fix them. Can anyone help?

In fle Dog.h:
[code]
#pragma once

class DogClass
{
public:
void setAge (int ageset); // Here it says: 'see declaration of 'DogClass::setAge'

void setWeight(int weightset); //The above error message is repeated here only it refers to setWeight
int getWeight();
int getAge();
DogClass(void);
~DogClass(void);
private:
int weight;
int age;
};
[code]

In File Dog.cpp:
[code]
#include "DogClass.h"
#include <iostream>
using namespace std;

DogClass::DogClass(void)
{
}

DogClass::~DogClass(void)
{
}

DogClass::setAge(int ageset)
{ //Here I get:'missing type specifier int assumed. Note: C++ does not support default-int' and 'int DogClass::setAge(int)' : overloaded function differs only by return type from 'void DogClass::setAge(int)' and 'DogClass::setAge':redefinition; different basic types'
age=ageset;
}

DogClass::setWeight(int weightset)
{ //The above Error messages are repeated here referencing the setWeight function.
weight=weightset;
}

DogClass::getAge()
{
return age;
}

DogClass::getWeight()
{
return weight;
}
[code]

In file testdog.cpp
[code]
#include "DogClass.h"
#include <iostream>
using namespace std;

int main()
{
DogClass Fido();
Fido.setWeight(5);
Fido.setAge(7);
cout<<"the dog weighs:"<<Fido.weight<<"\n";
cout<<"the dog's age is:"<<Fido.age<<"\n";
int pause;
cin>>pause;
return 0;
}
[code]

Any help would be greatly appreciated. Thanks in advance.
Last edited on
You need the return type in front of your methods when you define them. For instance:

1
2
3
4
void DogClass::setAge(int ageset)
{
//code
}


instead of

1
2
3
4
DogClass::setAge(int ageset)
{
//code
}


As you currently have it. I don't use visual c++, but I think that is one of your problems.
Thanks, that cleared up quite a bit. Now I get an error I'm getting a different error though:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "DogClass.h"
#include <iostream>
using namespace std;

int main()
{
	DogClass Fido();
	Fido.setWeight(5);     //error C2228: left of '.setWeight' must have class/struct/union
	
                Fido.setAge(7);          //error C2228: left of '.setAge' must have class/struct/union

	cout<<"the dog weighs:"<<Fido.weight<<"\n";  //error C2228: left of '.weight' must have class/struct/union

	cout<<"the dog's age is:"<<Fido.age<<"\n";  //error C2228: left of '.age' must have class/struct/union

	int pause;
	cin>>pause;
	return 0;
}


Any help there?
Last edited on
The first thing I see, that really doesn't seem to be part of your problem, is that in your cout statements you are trying to print out a private data member. The private access specifier only allows those variables to be accessed by member functions.
you have to change

 
void DogClass::getAge() {


with

 
int DogClass::getAge() {


and

 
DogClass::getWeight() {


with

 
int DogClass::getWeight() {


int the Dog.cpp

then you have to change

 
#include "DogClass.h" 


with

 
#include "Dog.cpp" 


in the file testdog.cpp

and

 
#include "DogClass.h" 


with

 
#include "Dog.h" 


in the file Dog.cpp

bye
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
// filename: Dog.cpp

#include "Dog.h"

DogClass::DogClass(void) {
}

DogClass::~DogClass(void) {
}

void DogClass::setAge(int ageset) {
	age=ageset;
}

void DogClass::setWeight(int weightset) {
	weight=weightset;
}

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

int DogClass::getWeight() {
	return weight;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
// filename: Dog.h

class DogClass {
	public:
		void setAge(int ageset);
		void setWeight(int weightset);
		int getWeight();
		int getAge();
		DogClass(void);
		~DogClass(void);
	private:
		int weight;
		int age;
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// filename: testdog.cpp

#include <iostream.h>
using namespace std;
#include "Dog.cpp"

int main() {
	DogClass Fido;
	Fido.setWeight(5);
	Fido.setAge(7);
	cout << "the dog weighs: " << Fido.getWeight() << "\n";
	cout << "the dog's age is: " << Fido.getAge() << "\n";
	int pause;
	cin >> pause;
	return 0;
}


This code works correctly.
Your help is very much appreciated. It finally compiled properly. The only changes I had to make to what you have above are the file names (they're named DogClass on my computer), and I had to change the

#include "DogClass.cpp"

back to

#include "DogClass.h

With the DogClass.cpp included in the main file, I kept on getting the error that the class members had already been defined. Anyway, thanks again.

Topic archived. No new replies allowed.