gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 00:46:56 25 4
gpt4 key购买 nike

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

感谢任何帮助

最佳答案

读这个:

1>c:\users\jorge\documents\visual studio 2010\projects\overriding\overriding\mammal.h(5): error C2011: 'Mammal' : 'class' type redefinition

现在,当您的程序编译时(从 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 和 #endif。

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com