I have the following header:
#include <string>
using namespace std;
enum COLOR {Green, Blue, White, Black, Brown};
class Animal{
private:
string _name;
COLOR _color;
public:
Animal();
~Animal();
void speak() const;
void move() const;
} ;
And the following .cpp implementation:
#include <iostream>
#include <string>
#include "Animal.h"
Animal::Animal(): _name("unknown")
{
cout << "constructing Animal object" << endl;
};
Animal::~Animal()
{
cout << "destructing Animal object" << endl;
}
void Animal::speak()
{
cout << "Animal speaks" << endl;
}
void Animal:: move(){};
However, the speak() and move() functions are giving me an error: "no declaration matches Animal::speak()" . If I remove the 'const' at the tail of the declaration, there are no issues in compilation. How do I correctly implement a const function in a .cpp file?
CodePudding user response:
You forget to put const
in the implementation.
Change your code to:
void Animal::speak() const
{
cout << "Animal speaks" << endl;
}
void Animal::move() const {};