2014-04-10 42 views
0

我得到了一些錯誤c3867(試圖編譯下面的代碼時,錯誤c3867,不知道我需要做修復

例如:

「錯誤C3867:「動物::睡眠':函數調用缺少參數列表;使用'&動物::睡眠'創建一個指向成員的指針'在第47行,hammertime.sleep;

我真的不知道爲什麼,我是新來的c + +到OOP,所以請和我一起...

#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; 
} 

任何幫助將大大appriciated

+2

該方法調用需要parens,如:'hammertime.sleep();' –

+0

你也想聲明睡眠(以及任何被它的孩子覆蓋的方法)爲虛擬的。 – Marius

回答

1

舉例來說,利用這個代碼:

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

newAnimal.sleepnewAnimal.eat的功能。要打電話給他們,您需要使用語法newAnimal.sleep()newAnimal.eat()