Johan Björklund
Johan Björklund

Reputation: 774

error c3867, not sure what i need to do to fix

I'm getting some error c3867( when trying to compile the code below,

for example:

"error C3867: 'Animal::sleep': function call missing argument list; use '&Animal::sleep' to create a pointer to member" at line 47, hammertime.sleep;

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

class Animal
{
protected:
    int age;
    string type;
public:
    void sleep() { cout << type << " : Sleeping" << endl; }
    void eat() {cout << type << " : Eating" << endl; }
    int getAge() { return age; }
    string getType() { return type; }
    Animal(int argAge) : age(argAge) {}
    Animal() : age(0),type("Animal") {}
    Animal::Animal(int, string);
};

class Lion : Animal
{
public:
    void sleep() { cout << "The lion is sleeping" << endl; }

};

class Hamster : Animal
{
public:
    void eat() { cout << "The hamster is eating" << endl; }
};

char YorN;
string aniType;
int eatOrSleep = 0;
int check = 0;


int main()
{
    Lion scar;
    scar.eat;

    Hamster hammertime;
    hammertime.sleep;

    cout << "Would you like to create a new animal? y/n" << endl;
    cin >> YorN;
    if (YorN == 'y' || YorN == 'Y'){
        cout << "What kind of animal?:" << endl;
        cin >> aniType;
        Animal newAnimal(0,aniType);
        cout << "Congratualtions, you just created a" << newAnimal.getType << endl;
        do
        {
            cout << "Enter either 1, 2 or 3:" << endl <<
                "1: Makes your animal sleep" << endl <<
                "2: Makes your animal eat" << endl <<
                "3: Exit the program" << endl;
            cin >> eatOrSleep;
            if (eatOrSleep == 1)
            {
                newAnimal.sleep;
            }
            else if (eatOrSleep == 2)
            {
                newAnimal.eat;
            }
            else if (eatOrSleep == 3)
            {
                check = 1;
                break;
            }
        } while (check == 0 );

    }

    return 0;
}

Upvotes: 0

Views: 455

Answers (1)

Freyja
Freyja

Reputation: 40904

For instance, take this code:

if (eatOrSleep == 1)
{
    newAnimal.sleep;
}
else if (eatOrSleep == 2)
{
    newAnimal.eat;
}

newAnimal.sleep and newAnimal.eat are functions. To call them, you need to use the syntax newAnimal.sleep() and newAnimal.eat().

Upvotes: 1

Related Questions