Dec 11, 2018 at 12:01am UTC
The only error that appears in my code when I try to run it and I am having trouble figuring out how to fix it. I receive the error in my all my header files in the line
animalType = "lizard" ;
Aninmal.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#ifndef ANIMAL_H_
#define ANIMAL_H_
#include <string>
#include <iostream>
using namespace std;
class animal
{
public :
animal();
virtual string talk() = 0;
virtual string move() = 0;
//Support << operator, outputs the animal type, talk, and move
friend ostream &operator <<(ostream &output, animal &lhs);
char *animalType;
static int numAnimals;
virtual ~animal() = 0;
};
#endif /* ANIMAL_H_ */
Animal.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include "animal.h"
using namespace std;
animal::animal()
{
numAnimals = numAnimals + 1;
}
//Overload our cout operator, to support printing our animal
ostream &operator <<(ostream &output, animal &lhs)
{
output << lhs.animalType << ", " << lhs.talk() << ", " << lhs.move();
return output;
}
animal::~animal()
{
--numAnimals;
}
Lizard.h
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
#include "Lizard.h"
using namespace std;
lizard::lizard()
{
animalType = "lizard" ;
}
string lizard::talk()
{
return "meep" ;
}
string lizard::move()
{
return "run" ;
}
//Overload our cout operator, to support printing our animal
ostream &operator <<(ostream &output, lizard &lhs)
{
output << lhs.animalType << ", " << lhs.talk() << ", " << lhs.move();
return output;
}
lizard::~lizard()
{
// TODO Auto-generated destructor stub
}
Last edited on Dec 11, 2018 at 12:02am UTC
Dec 11, 2018 at 12:38am UTC
Why is animalType declared to be a char*? Shouldn't it be a string?
Dec 11, 2018 at 2:38pm UTC
it should be a string.
you can't assign char* directly.
you can use strcpy (a C function) for this but make sure the char* has memory or that will crash. Handling C style strings is a bit of work, do you need to use them/ need to learn that?
Last edited on Dec 11, 2018 at 2:39pm UTC