我的获取函数不起作用
My get function isn't working
class Cat {
public:
string getname() const;
void setname(string name);
private:
string name;
// constructor
Cat(string name) {
this->name = name;
cout<<"Cat's name is "<< name << endl;
}
};
int main Cat::getname() {
string name ="Assignment 09";
cout << name << endl;
Dog fido("Fido");
Cat spot("Spot");
cout <<"From main, the Dog's name is "<< fido.name << endl;
cout <<"From main, the Cat's name is "<< spot.name << endl;
cout <<"Hit any key to continue"<< endl;
system("pause");
return name;
}
要从对象中获取名称,需要使用getname
方法:
// Note the removal of Cat::getname()
int main (void)
{
string name ="Assignment 09";
cout << name << endl;
Dog fido("Fido");
Cat spot("Spot");
// Note the changes here: fido.getname()
cout <<"From main, the Dog's name is "<< fido.getname() << endl;
// Similarly: spot.getname()
cout <<"From main, the Cat's name is "<< spot.getname() << endl;
cout <<"Hit any key to continue"<< endl;
system("pause");
return name;
}
fido.getname()
是一个假设,因为您在问题中没有提供Dog
的类定义。
相关文章:
- C++为什么尽管我调用了void函数,它却不起作用
- 仅包含可移动 std::map 的类的移动构造函数不起作用
- 通过构造函数创建一些值并尝试添加到文档中使用 rapidjson 不起作用
- 为什么我的数组双精度函数不起作用?
- 使用 va_arg 传递给函数va_list不起作用
- Windows.h 输入在函数之外不起作用
- 类和构造函数中的函数根本不起作用,并且不返回任何错误
- 类中的虚拟布尔函数参数不起作用
- getline 函数似乎在 C++ 中不起作用
- 将新放置与 std::函数一起使用不起作用
- binary_search() 函数在我的函数体中不起作用
- 为什么读取函数在 sha1 c++ 实现中不起作用?
- 在函数中声明的 ifStream 对象在发布模式下不起作用
- 指针变量在 cout 函数中不起作用
- std::函数不起作用,但普通的旧函数指针可以 - 为什么?
- C++ 函数,用于查找数组中四个最小最大元素的总和不起作用
- 为什么必须使用'*p=a',而使用'p=&a'在构造函数中不起作用
- C++ Arduino - 随机函数不起作用
- QObject::连接不起作用 - 使用函数语法找不到信号
- 函数在类中不起作用-函数调用缺少参数列表