为什么这个程序没有预期的结果?

来源:百度知道 编辑:UC知道 时间:2024/06/20 15:48:10
这个简单的C++程序:
#include <iostream>
class Cat
{
public:
int GetAge();
void SetAge (int age);
void Meow();
private:
int itsAge;
};

int Cat::GetAge()
{
return itsAge;
}

void Cat::SetAge (int age)
{
itsAge = age ;
}

void Cat::Meow()
{
std::cout << "Meow.\n";
}

int main()
{
Cat Frisky;
Frisky.SetAge( 5 );
Frisky.Meow();
std::cout << "Frisky is a cat who is ";
std::cout << Frisky.GetAge << " years old.\n";
Frisky.Meow();
return 0;
}

很显然,运行的结果应该是:
Meow.
Frisky is a cat who is 5 years old.
Meow.

可是,我得出的结果却是:不管源代码中怎样,输出总是 1 years old……

遭遇灵异事件,请求大家帮忙解答!

std::cout << Frisky.GetAge << " years old.\n";
GetAge是个函数,后边的小括号不能少

就算这个程序被你编译通过了,它也会有个warning的,你难道就没用关注下?

LS已经指出错误的所在了,就不重复了