C++主文件如何初始化类函数及解决Animal类无display成员报错
C++动物谷仓管理程序问题修复方案
核心问题解决
1. 类初始化与主函数联动问题
C++要求类的声明必须放在使用位置之前,原代码将main函数写在所有类定义之前,导致main函数无法识别Barn、Animal等类类型。调整类定义顺序到main之前后,直接在main中实例化Barn对象即可完成初始化,Barn的构造函数会自动完成动物实例的创建工作。
2. class Animal has no member named 'display'报错问题
报错原因是Animal类中没有定义
display()成员函数,只需在Animal类的public区域添加display方法,返回拼接好的动物信息字符串即可。
其他隐藏问题修复
feedAnimal成员函数定义错误嵌套在Barn构造函数内部,调整到构造函数外部- 动物属性调用错误:喂动物输出信息时,未通过
barn[i]指针调用属性,且getWeight方法名大小写拼写错误 - 循环变量名冲突:Barn构造函数中添加动物的外层循环和遍历动物的内层循环都用了i作为变量名
- 喂动物输出逻辑错误:输出增重信息的代码未放在饲喂判断分支内,会导致所有动物都重复输出
- 主函数仅执行一次就退出:添加循环逻辑支持用户多次操作
- 内存泄漏:添加Barn类析构函数,释放所有动态创建的Animal对象
修正后完整代码
// C++ program to create classes for Barn of Animals #include <iostream> #include <string> #include <cstdlib> #include <ctime> #include <vector> using namespace std; // 基类 Animal class Animal { private: string type; string name; int weight; public: Animal(string type, string name, int weight): type(type), name(name), weight(weight) {} string getType() { return type; } string getName() { return name; } int getWeight() { return weight; } void setWeight(int weight) { this->weight = weight; } // 新增display方法 string display() { return "类型:" + type + ",姓名:" + name + ",体重:" + to_string(weight) + "lbs"; } }; // 派生类 Horse class Horse: public Animal { public: Horse(string name, int weight): Animal("horse", name, weight) {} }; // 派生类 Cow class Cow: public Animal { public: Cow(string name, int weight): Animal("cow", name, weight) {} }; // 派生类 Chicken class Chicken: public Animal { public: Chicken(string name, int weight): Animal("chicken", name, weight) {} }; class Barn { private: vector<Animal*> barn; public: Barn(); ~Barn(); // 新增析构函数释放内存 void feedAnimal(); void displayAnimals(); // 新增动物列表展示方法 }; Barn::Barn() { srand(time(0)); Animal * a; for (int i = 0; i < 5; i++) { a = new Horse("Artax" + to_string(i + 1), rand() % 200 + 1820); barn.push_back(a); a = new Cow("Fafnir" + to_string(i + 1), rand() % 250 + 1200); barn.push_back(a); a = new Chicken("David" + to_string(i + 1), rand() % 2 + 15); barn.push_back(a); } } // 析构函数实现 Barn::~Barn() { for (auto animal : barn) { delete animal; } barn.clear(); } // 动物列表展示方法实现 void Barn::displayAnimals() { for (int j = 0; j < barn.size(); j++) { cout << barn[j]->display() << endl; } } // feedAnimal方法移到构造函数外部实现 void Barn::feedAnimal() { int days; cout << "请输入模拟天数:"; cin >> days; for (int day = 0; day < days; day++) { cout << "\n===== 第" << day + 1 << "天 =====" << endl; for (int i = 1; i <= 12; i++) { string feed = to_string(i) + ":00"; cout << "当前时间:" << feed << "\n"; if (i == 3) // 3点喂鸡 { for (size_t j = 0; j < barn.size(); j++) { if (barn[j]->getType() == "chicken") { barn[j]->setWeight(barn[j]->getWeight() + 1); cout << barn[j]->getName() << " " << barn[j]->getType() << " 进食后体重为 " << barn[j]->getWeight() << " lbs\n"; } } } else if (i == 5) // 5点喂马 { for (size_t j = 0; j < barn.size(); j++) { if (barn[j]->getType() == "horse") { barn[j]->setWeight(barn[j]->getWeight() + 5); cout << barn[j]->getName() << " " << barn[j]->getType() << " 进食后体重为 " << barn[j]->getWeight() << " lbs\n"; } } } else if (i == 7) //7点喂牛 { for (size_t j = 0; j < barn.size(); j++) { if (barn[j]->getType() == "cow") { barn[j]->setWeight(barn[j]->getWeight() + 5); cout << barn[j]->getName() << " " << barn[j]->getType() << " 进食后体重为 " << barn[j]->getWeight() << " lbs\n"; } } } } } } int main() { Barn myBarn; // 初始化谷仓对象 int choice; do { cout << "\n===== 谷仓管理菜单 =====" << endl; cout << "1) 模拟运行天数" << endl; cout << "2) 展示所有动物信息" << endl; cout << "3) 退出" << endl; cout << "请输入选项1/2/3:"; cin >> choice; switch (choice) { case 1: myBarn.feedAnimal(); break; case 2: myBarn.displayAnimals(); break; case 3: cout << "程序已退出" << endl; break; default: cout << "输入无效,请输入1、2或3" << endl; break; } } while(choice != 3); return 0; }
内容的提问来源于stack exchange,提问作者Gabriel Sharp
相关产品推荐
相关产品推荐

