无法从主调用类的函数

Cannot call function of a class from main

本文关键字:函数 调用      更新时间:2023-10-16
#include <iostream> 
#include <string> 
using namespace std; 
// my code starts
class Cat {
public:
int age;
string name, race, voice;
Cat(int age2,string name2,string race2,string voice2);
void PrintInformation();
};
Cat::Cat(int age2,string name2,string race2,string voice2) {
    age = age2;
    name = name2;
    race = race2;
    voice = voice2;
}
Cat::Meow(){
    cout << "Cat says: " << fluffy.Meow() << endl;
}

void Cat::PrintInformation() {
    cout << "Name: " << name;
    cout << "nAge: " << age;
    cout << "nRace: " << race << endl;
  }
// my code ends
int main() 
{ 
Cat fluffy(2, "Fluffy", "Bombay", "Meoow!!!"); 
fluffy.PrintInformation(); 
cout << "Cat says: " << fluffy.Meow(); 
}  

我似乎无法弄清楚如何使这段代码工作。我的主要问题似乎是我不知道如何从int main()打电话给fluffy.Meow();.谢谢,任何帮助!

你忘了在类声明中声明Cat::Meow

//some code
void PrintInformation();
void Meow();

此外,您必须指定函数Meow的返回类型是什么,在您的情况下,它将是 void ,因为它不返回任何内容。

你也有一些递归正在进行,Meow调用Meow(忘记了fluffy不是这个范围内的变量)。您的Cat类对实例fluffy一无所知,因此您无法访问它。

我猜你的意思是voice