why am I getting errors with the Mammal.h class when compiling
Guys, why am I getting errors here? The following was done in Visual C++
Mammal.h class file
#include <iostream>
using namespace std;
class Mammal
{
public:
//constructor
Mammal() { cout << "Mammal constructor...\n"; }
Mammal(int age) { maAge = age; cout << "Mammal age constructor...\n"; }
~Mammal() { cout << "Mammal destructor...\n"; }
//accessors
void setMaAge(int age) { maAge = age; }
void setMaWeight(int weight) { maWeight = weight; }
int getMaAge() { return maAge; }
int getMaWeight() { return maWeight; }
//other func
void speak() { cout << "Mammal Sound!...\n"; }
void maSleep() { cout << "Mammal Sleeping!...\n"; }
protected:
int maAge, maWeight;
};
Dog.h class file
#include <iostream>
#include "Mammal.h"
using namespace std;
enum BREED { golden, shepard, lab, doberman };
class Dog : public Mammal
{
public:
//constructor
Dog() { cout << "Constructing dog!...\n"; }
Dog(int age) { Mammal(age); cout << "Constructing dog with age!...\n"; }
Dog(int age, BREED breed) { Mammal(age); dogBreed = breed;
cout << "Constructing dog with age and breed!... \n"; }
Dog(int age, int weight) { Mammal(age); maWeight = weight;
cout << "Constructing dog with age and weight!...\n"; }
~Dog(){ cout << "Destruction of dog!...\n"; }
//accessors
void setDogAge(int age) { maAge = age; }
void setDogWeight(int weight) { maWeight = weight; }
void setDogBreed(BREED breed) { dogBreed = breed; }
int getDogAge() { return maAge; }
int getDogWeight() { return maWeight; }
int getDogBreed() { return dogBreed; }
//other func
void dogWagTail() { cout << "Dog Wagging Tail!...\n"; }
void dogBegFood() { cout << "Dos is Hungry!... Wants a bone.\n"; }
void speak() { cout << "Dog Sound: Wof, Wof!\n"; }
private:
BREED dogBreed;
};
main.cpp file
#include <iostream>
#include "Mammal.h"
#include "Dog.h"
using namespace std;
int main()
{
Mammal fourLegs;//const mammal
Dog Champ;//const dogs
Dog Pimp(4, lab);
Dog Mango(2, 54);
fourLegs.speak();
Champ.speak();
Champ.setDogBreed(doberman);
Pimp.setDogWeight(123);
cout << "Pimp is: " << Pimp.getDogAge() << " years old - Weight: " << Pimp.getDogWeight()
<< " pounds - breed: " << Pimp.getDogBreed() << endl;
Mango.dogWagTail();
system("pause");
return 0;
}
errors:
1>------ Build started: Project: Overriding, Configuration: Debug Win32 ------
1> main.cpp
1>c:\users\jorge\documents\visual studio 2010\projects\overriding\overriding\mammal.h(5): error C2011: 'Mammal' : 'class' type redefinition
1> c:\users\jorge\documents\visual studio 2010\projects\overriding\overriding\mammal.h(5) : see declaration of 'Mammal'
1>c:\users\jorge\documents\visual studio 2010\projects\overriding\overriding\dog.h(6): error C2504: 'Mammal' : base class undefined
1>c:\users\jorge\documents\visual studio 2010\projects\overriding\overriding\dog.h(10): error C2079: 'age' uses undefined class 'Mammal'
1>c:\users\jorge\documents\visual studio 2010\projects\overriding\overriding\dog.h(10): error C2082: redefinition of formal parameter 'age'
1>c:\users\jorge\documents\visual studio 2010\projects\overriding\overriding\dog.h(11): error C2079: 'age' uses undefined class 'Mammal'
1>c:\users\jorge\documents\visual st开发者_运维知识库udio 2010\projects\overriding\overriding\dog.h(11): error C2082: redefinition of formal parameter 'age'
1>c:\users\jorge\documents\visual studio 2010\projects\overriding\overriding\dog.h(13): error C2079: 'age' uses undefined class 'Mammal'
1>c:\users\jorge\documents\visual studio 2010\projects\overriding\overriding\dog.h(13): error C2082: redefinition of formal parameter 'age'
1>c:\users\jorge\documents\visual studio 2010\projects\overriding\overriding\dog.h(13): error C2065: 'maWeight' : undeclared identifier
1>c:\users\jorge\documents\visual studio 2010\projects\overriding\overriding\dog.h(17): error C2065: 'maAge' : undeclared identifier
1>c:\users\jorge\documents\visual studio 2010\projects\overriding\overriding\dog.h(18): error C2065: 'maWeight' : undeclared identifier
1>c:\users\jorge\documents\visual studio 2010\projects\overriding\overriding\dog.h(20): error C2065: 'maAge' : undeclared identifier
1>c:\users\jorge\documents\visual studio 2010\projects\overriding\overriding\dog.h(21): error C2065: 'maWeight' : undeclared identifier
1>c:\users\jorge\documents\visual studio 2010\projects\overriding\overriding\main.cpp(8): error C2079: 'fourLegs' uses undefined class 'Mammal'
1>c:\users\jorge\documents\visual studio 2010\projects\overriding\overriding\main.cpp(12): error C2228: left of '.speak' must have class/struct/union
1> type is 'int'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
any help appreciated
Read this:
1>c:\users\jorge\documents\visual studio 2010\projects\overriding\overriding\mammal.h(5): error C2011: 'Mammal' : 'class' type redefinition
Now, when your program compiles (starts from main.cpp) it includes iostream, then Mammal.h, then Dog.h.
When including Mammal.h and Dog.h program includes also all the includes from those files also.
So, Mammal.h is included first from main.cpp and then from Dog.h
To remove this error, simply use #ifndef
, #define
and #endif
like this:
#ifndef _MAMMAL_H
#define _MAMMAL_H
#include <iostream>
using namespace std;
class Mammal
{
public:
//constructor
Mammal() { cout << "Mammal constructor...\n"; }
Mammal(int age) { maAge = age; cout << "Mammal age constructor...\n"; }
~Mammal() { cout << "Mammal destructor...\n"; }
//accessors
void setMaAge(int age) { maAge = age; }
void setMaWeight(int weight) { maWeight = weight; }
int getMaAge() { return maAge; }
int getMaWeight() { return maWeight; }
//other func
void speak() { cout << "Mammal Sound!...\n"; }
void maSleep() { cout << "Mammal Sleeping!...\n"; }
protected:
int maAge, maWeight;
};
#endif
Always include #ifndef #define AND #endif in all your .h files.
Besides inclusion guards, in Dog
object you are initializing Mammal
object wrong way, you should initiliaze your base class like this;
Dog(int age) : Mammal(age) { ... }
You should be using include guards in your headers.
main.cpp is including Mammal.h before including Dog.h which itself includes Mammal.h. This means that the Mammal class is being redefined within main, hence the compiler errors. Include guards protect you from this.
you should protect your headers
#ifndef _MAMMAL_H_
#define _MAMMAL_H_
class Mammal {
...
};
#endif
#ifndef _DOG_H_
#define _DOG_H_
class Dog {
...
};
#endif
精彩评论