伙计们,为什么我在这里出错?
以下是在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 ==========

任何帮助表示赞赏

最佳答案

读这个:



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

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

因此,首先从main.cpp中包含Mammal.h,然后从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和#endif。

关于c++ - 为什么在编译时出现Mammal.h类错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4569122/

10-15 01:06