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.
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>
usingnamespace 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;
}
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.
// 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>
usingnamespace 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;
}
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.