为什么我在编译时遇到Mammal.h类的错误

时间:2010-12-31 09:00:22

标签: c++ visual-c++

伙计们,我为什么在这里收到错误? 以下是在Visual C ++中完成的

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;
};

Dog.h类文件

#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文件

#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;
}

错误:

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 studio 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 ==========

任何帮助表示赞赏

4 个答案:

答案 0 :(得分:3)

阅读本文:

  

1以及c:\用户\豪尔赫\文件\视觉   工作室   2010 \项目\首要\首要\ mammal.h(5):   错误C2011:'哺乳动物':'类'类型   重新定义

现在,当你的程序编译时(从main.cpp开始)它包含iostream,然后是Mammal.h,然后是Dog.h。

当包括Mammal.h和Dog.h程序时,还包括来自这些文件的所有包含。

因此,Mammal.h首先包含在main.cpp中,然后来自Dog.h

要删除此错误,只需使用#ifndef#define#endif,如下所示:

#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

始终在所有.h文件中包含#ifndef #define AND #endif。

答案 1 :(得分:2)

除了包含守卫,在Dog对象中你正在以错误的方式初始化Mammal对象,你应该像这样初始化你的基类;

Dog(int age) : Mammal(age) { ... }

答案 2 :(得分:0)

您应该在标题中使用include guards

main.cpp包括Mammal.h,之后包括Dog.h,其本身包括Mammal.h。这意味着Mammal类正在main中重新定义,因此编译器错误。包括警卫保护您免受此事。

答案 3 :(得分:0)

你应该保护你的标题

#ifndef _MAMMAL_H_
#define _MAMMAL_H_

class Mammal {
...
};

#endif



#ifndef _DOG_H_
#define _DOG_H_

class Dog {
...
};

#endif