Jul 3, 2019 at 7:11pm UTC
I am trying to learn C++ and when I compile I am getting the following error messages:
[Error] invalid use of incomplete type 'class Mammal'
[Error] forward declaration of 'class Mammal'
[Error] extra qualification 'Mammal::' of member 'Mammal' [-fpermissive]
[Error] 'Mammal::Mammal()' cannot be overloaded
[Error] with 'Mammal::Mammal()'
I want to know where is my mistake?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
//Listing 12.3 Constructors and destructors called
#include <iostream>
enum BREED { GOLDEN, CAIRN, CANDIE, SHETLAND, DOBERMAN, LAB };
class Mammal
{
public :
// constructors
Mammal();
~Mammal();
//accessors
int GetAge() const { return itsAge; )
void SetAge(int age) { itsAge = age; }
int GetWeight() const { return itsWeight; }
void SetWeight(int weight) { itsWeight = weight; }
//other methods
void Speak()const { std::cout << "Mammal sound!\n" ; }
void Sleep()const { std::cout << "shhh, I'm sleeping.\n" ; }
protected :
int itsAge;
int itsWeight;
};
class Dog : public Mammal
{
public :
// constructors
Dog();
~Dog();
// accessors
BREED GetBreed() const { return itsBreed; }
void SetBreed(BREED breed) { itsBreed = breed; }
// other methods
void WagTail() const { std::cout << "Tail wagging...\n" ; }
void BegForFood() const { std::cout << "Begging for food...\n" ; }
private :
BREED itsBreed;
};
Mammal::Mammal():
itsAge(),
itsWeight(5)
{
std::cout << "Mammal constructor...\n" ;
}
Mammal::~Mammal()
{
std::cout << "Mammal destructor...\n" ;
}
Dog::Dog():
itsBreed(GOLDEN)
{
std::cout << "Dog constructor...\n" ;
}
Dog::~Dog()
{
std::cout << "Dog destructor...\n" ;
}
int main()
{
Dog fido;
fido.Speak();
fido.WagTail();
std::cout << "fido is " << fido.GetAge() << " years old\n" ;
return 0
}
Last edited on Jul 3, 2019 at 7:47pm UTC
Jul 3, 2019 at 7:20pm UTC
Look closely at every single character on line 14.
EDIT: I forgot to mention the <iostream.h>
! I think that's deprecated. As Niccolo says, use <iostream>
and then, if you must, use namespace std;
.
Last edited on Jul 3, 2019 at 7:33pm UTC
Jul 3, 2019 at 7:23pm UTC
@mekbibb11,
Sometimes the messages aren't exactly what they claim, but it is important to know which lines are referenced by the errors.
My own read through, with a compiler fire-breathing errors and warnings (a typical experience we just must get used to), comes to these points:
1 2 3 4
#include <iostream>
not iostream.h!
In accessors
int GetAge() const { return itsAge; ) <***** this must be a close bracket, not close paren
In main
return 0 <**********this must end with a ';'
....and, of course, you can only have one "main" function
When I corrected that exact series of observations, the code compiled.
Last edited on Jul 3, 2019 at 7:25pm UTC
Jul 3, 2019 at 7:27pm UTC
Line 14: Replace the right parenthesis )
with a curly bracket }
Jul 3, 2019 at 7:40pm UTC
sorry for the iostream.h but still my question is still the same. I edited the the in include to <iostream>.
thanks Niccola
Jul 3, 2019 at 7:43pm UTC
Can you read? You've been told three times.
Jul 3, 2019 at 7:49pm UTC
Thanks so much Niccola. I saw my errors. That was time saver. Thanks for the help. If I still get errors I may still come for help.
Thanks again.