用主文件中的类初始化函数的方法是什么
What is the way to initialize a function with classes in the main file?
此程序是一个c++项目,用于使用畜棚和动物的类创建动物畜棚。这些动物通过它们的名字、类型和体重来识别。在特定的时间,动物会进食并增加体重。用户可以选择经过多少天。大部分代码都已经编写完成,现在只需要让main使用类函数。当我尝试在main中执行函数时,我会得到错误代码";main.cpp:117:28:错误:"class Animal"没有名为"display"的成员"关于表示:cout<lt;谷仓[i]->display((这是代码。
// C++ program to create classes for Barn of Animals
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <vector>
using namespace std;
int main()
{
//I need to run the program so a unit of time passes
//When the time runs, the time will be compared to
//the animals feeding time. When that feed time passes,
//The animal gains weight and the new weight is shown.
int choice;
cout << "1) Simulate Days" << endl;
cout << "2) Display Animals" << endl;
cout << "3) Exit" << endl;
cout << "Choose an option: n";
cout << "Enter 1, 2 or 3: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Choice 1";
break; //Here we will run feedAnimal
case 2:
cout << "Choice 2";
break; //Here we will display all the animals
case 3:
cout << "Choice 3";
break; //Here we will exit the program
default:
cout << "Not 1, 2 or 3";
break;
}
}
// base Animal class
class Animal
{
private:
// data members
string type;
string name;
int weight;
public:
// constructor to initialize the members to specified values
Animal(string type, string name, int weight): type(type), name(name), weight(weight) {}
// getters
string getType()
{
return type;
}
string getName()
{
return name;
}
int getWeight()
{
return weight;
}
// setter for weight
void setWeight(int weight)
{
this->weight = weight;
}
};
// derived class Horse
class Horse: public Animal
{
public: Horse(string name, int weight): Animal("horse", name, weight) {}
};
// derived class Cow
class Cow: public Animal
{
public: Cow(string name, int weight): Animal("cow", name, weight) {}
};
// derived class Chicken
class Chicken: public Animal
{
public: Chicken(string name, int weight): Animal("chicken", name, weight) {}
};
class Barn
{
private:
vector<Animal*> barn;
public:
Barn();
void feedAnimal();
};
Barn::Barn()
{
srand(time(0)); // this will be srand not rand
Animal * a;
for (int i = 0; i < 5; i++)
{
a = new Horse("Artax" + to_string(i + 1), rand() % 200 + 1820); // end with semicolon not colon
barn.push_back(a);
a = new Cow("Fafnir" + to_string(i + 1), rand() % 250 + 1200); // convert (i+1) to string not (i+)
barn.push_back(a);
a = new Chicken("David" + to_string(i + 1), rand() % 2 + 15);
barn.push_back(a);
for (int i = 0; i < barn.size(); i++)
{
cout << barn[i]->display() << " " << " "; //This Displays all the animals
}
}
void Barn::feedAnimal()
{
int days; // = 0;
cout << "how many days:";
cin >> days;
for (int day = 0; day < days; day++)
{
for (int i = 1; i <= 12; i++)
{
string feed = to_string(i) + ":00"; // define the type for feed
cout << "The Time is " << feed << "n";
if (i == 3) // time is 3:00, feed the chickens
{
// loop over the vector of Animals to feed the Chicken
for (size_t i = 0; i < barn.size(); i++)
{
if (barn[i]->getType() == "chicken") // this animal is chicken
barn[i]->setWeight(barn[i]->getWeight() + 1); // add 1 pound to its weight
cout << name << " " << type << " now weighs " << getweight() << " lbs";
}
}
else if (i == 5) // time is 5:00, feed the horses
{
// loop over the vector of Animals to feed the Horse
for (size_t i = 0; i < barn.size(); i++)
{
if (barn[i]->getType() == "horse") // this animal is horse
barn[i]->setWeight(barn[i]->getWeight() + 5); // add 5 pound to its weight
cout << name << " " << type << " now weighs " << getweight() << " lbs";
}
}
else if (i == 7) // time is 7:00, feed the cows
{
// loop over the vector of Animals to feed the Cow
for (size_t i = 0; i < barn.size(); i++)
{
if (barn[i]->getType() == "cow") // this animal is cow
barn[i]->setWeight(barn[i]->getWeight() + 5); // add 5 pound to its weight
cout << name << " " << type << " now weighs " << getweight() << " lbs";
}
}
}
}
}
}
运算符<lt;可能过载:
#include <iostream>
class MyClass {
int i = 0;
public:
MyClass() : i(4) { }
friend std::ostream& operator<<(std::ostream& out, MyClass obj) {
out << obj.i;
return out;
}
};
int main() {
MyClass myClass;
std::cout << myClass;
return 0;
}
在类Animal
中,您只需实现返回字符串的display()
方法,并且在更改后一切正常:
std::string display() const {
std::stringstream ss;
ss << "type: " << type << ", name: " << name
<< ", weights: " << weight << std::endl;
return ss.str();
}
下面的完整更正代码。还必须修复一些拼写错误以使代码可编译:
在线试用!
// C++ program to create classes for Barn of Animals
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <sstream>
using namespace std;
int main()
{
//I need to run the program so a unit of time passes
//When the time runs, the time will be compared to
//the animals feeding time. When that feed time passes,
//The animal gains weight and the new weight is shown.
int choice;
cout << "1) Simulate Days" << endl;
cout << "2) Display Animals" << endl;
cout << "3) Exit" << endl;
cout << "Choose an option: n";
cout << "Enter 1, 2 or 3: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Choice 1";
break; //Here we will run feedAnimal
case 2:
cout << "Choice 2";
break; //Here we will display all the animals
case 3:
cout << "Choice 3";
break; //Here we will exit the program
default:
cout << "Not 1, 2 or 3";
break;
}
}
// base Animal class
class Animal {
private:
// data members
string type;
string name;
int weight;
public:
// constructor to initialize the members to specified values
Animal(string type, string name, int weight): type(type), name(name), weight(weight) {}
// getters
string getType()
{
return type;
}
string getName()
{
return name;
}
int getWeight()
{
return weight;
}
// setter for weight
void setWeight(int weight)
{
this->weight = weight;
}
std::string display() const {
std::stringstream ss;
ss << "type: " << type << ", name: " << name
<< ", weights: " << weight << std::endl;
return ss.str();
}
};
// derived class Horse
class Horse: public Animal
{
public: Horse(string name, int weight): Animal("horse", name, weight) {}
};
// derived class Cow
class Cow: public Animal
{
public: Cow(string name, int weight): Animal("cow", name, weight) {}
};
// derived class Chicken
class Chicken: public Animal
{
public: Chicken(string name, int weight): Animal("chicken", name, weight) {}
};
class Barn
{
private:
vector<Animal*> barn;
public:
Barn();
void feedAnimal();
};
Barn::Barn() {
srand(time(0)); // this will be srand not rand
Animal * a;
for (int i = 0; i < 5; i++)
{
a = new Horse("Artax" + to_string(i + 1), rand() % 200 + 1820); // end with semicolon not colon
barn.push_back(a);
a = new Cow("Fafnir" + to_string(i + 1), rand() % 250 + 1200); // convert (i+1) to string not (i+)
barn.push_back(a);
a = new Chicken("David" + to_string(i + 1), rand() % 2 + 15);
barn.push_back(a);
for (int i = 0; i < barn.size(); i++)
{
cout << barn[i]->display() << " " << " "; //This Displays all the animals
}
}
}
void Barn::feedAnimal()
{
int days; // = 0;
cout << "how many days:";
cin >> days;
for (int day = 0; day < days; day++)
{
for (int i = 1; i <= 12; i++)
{
string feed = to_string(i) + ":00"; // define the type for feed
cout << "The Time is " << feed << "n";
if (i == 3) // time is 3:00, feed the chickens
{
// loop over the vector of Animals to feed the Chicken
for (size_t i = 0; i < barn.size(); i++)
{
if (barn[i]->getType() == "chicken") // this animal is chicken
barn[i]->setWeight(barn[i]->getWeight() + 1); // add 1 pound to its weight
//cout << name << " " << type << " now weighs " << getWeight() << " lbs";
cout << barn[i]->display();
}
}
else if (i == 5) // time is 5:00, feed the horses
{
// loop over the vector of Animals to feed the Horse
for (size_t i = 0; i < barn.size(); i++)
{
if (barn[i]->getType() == "horse") // this animal is horse
barn[i]->setWeight(barn[i]->getWeight() + 5); // add 5 pound to its weight
//cout << name << " " << type << " now weighs " << getWeight() << " lbs";
cout << barn[i]->display();
}
}
else if (i == 7) // time is 7:00, feed the cows
{
// loop over the vector of Animals to feed the Cow
for (size_t i = 0; i < barn.size(); i++)
{
if (barn[i]->getType() == "cow") // this animal is cow
barn[i]->setWeight(barn[i]->getWeight() + 5); // add 5 pound to its weight
//cout << name << " " << type << " now weighs " << getWeight() << " lbs";
cout << barn[i]->display();
}
}
}
}
}
好的,下面是我如何让它工作的。谢谢你的帮助,它真的帮助了我。
//Fixed Practice code
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <vector>
using namespace std;
// base Animal class
class Animal {
private:
// data members
string type;
string name;
int weight;
public:
// constructor to initialize the members to specified values
//Animal(string type, string name, int weight) : type(type), name(name), weight(weight)
Animal(string type, string name, int weight) {
//Added code to called the setters
setWeight(weight);
setName(name);
setType(type);
}
// getters
string getType() { return type; }
string getName() { return name; }
int getWeight() { return weight; }
//Added setter for type
void setType(string type) { this->type = type; }
//Added setter for name
void setName(string name) { this->name = name; }
// setter for weight
void setWeight(int weight) { this->weight = weight; }
//Added the display method in the Animal class to display the animal details
string display() {
string printLine = "";
printLine = "nAnimal Type: " + this->getType() + ", Animal Name: " + this->getName() + ", Animal Weight: " + to_string(this->getWeight());
return printLine;
}
};
// derived class Horse
class Horse : public Animal {
public:
Horse(string name, int weight) : Animal("horse", name, weight)
{}
};
// derived class Cow
class Cow : public Animal {
public:
Cow(string name, int weight) : Animal("cow", name, weight)
{}
};
// derived class Chicken
class Chicken : public Animal {
public:
Chicken(string name, int weight) : Animal("chicken", name, weight)
{}
};
class Barn {
private:
vector<Animal*> barn;
public:
Barn();
void feedAnimal();
void display();
};
Barn::Barn() {
srand(time(0)); // this will be srand not rand
Animal* a;
for (int i = 0; i < 5;i++) {
a = new Horse("Artax" + to_string(i + 1), rand() % 200 + 1820); // end with semicolon not colon
barn.push_back(a);
a = new Cow("Fafnir" + to_string(i + 1), rand() % 250 + 1200); // convert (i+1) to string not (i+)
barn.push_back(a);
a = new Chicken("David" + to_string(i + 1), rand() % 2 + 15);
barn.push_back(a);
}
}
//Added the display code here, moved the code from inside the Barn constructor
void Barn::display() {
for (int i = 0; i < barn.size(); i++) {
cout << barn[i]->display() << " " << " "; //This Displays all the animals
}
}
void Barn::feedAnimal() {
int days;// = 0;
string feed;
cout << "nhow many days:";
cin >> days;
//Added the code here - Sort of modifications
//We are looping the vector once, check the time and animal increase the weight, the prior code was looping the vector multiple times for a day and time
for (int day = 0; day < days;day++) {
for (int feedtime = 1; feedtime <= 12; feedtime++) {
feed = to_string(feedtime) + ":00"; // define the type for feed
cout << "nThe Time is " << feed << "n";
for (int barnNum = 0; barnNum < barn.size();barnNum++) {
if ((feedtime == 3) && (barn[barnNum]->getType() == "chicken")) {
barn[barnNum]->setWeight(barn[barnNum]->getWeight() + 1); // add 1 pound to its weight
cout << "n" << barn[barnNum]->getName() << " " << barn[barnNum]->getType() << " now weighs " << barn[barnNum]->getWeight() << " lbs";
}
if ((feedtime == 5) && (barn[barnNum]->getType() == "horse")) {
barn[barnNum]->setWeight(barn[barnNum]->getWeight() + 5); // add 5 pound to its weight
cout << "n" << barn[barnNum]->getName() << " " << barn[barnNum]->getType() << " now weighs " << barn[barnNum]->getWeight() << " lbs";
}
if ((feedtime == 7) && (barn[barnNum]->getType() == "cow")) {
barn[barnNum]->setWeight(barn[barnNum]->getWeight() + 5); // add 5 pound to its weight
cout << "n" << barn[barnNum]->getName() << " " << barn[barnNum]->getType() << " now weighs " << barn[barnNum]->getWeight() << " lbs";
}
}
}
}
}
int main()
{
//I need to run the program so a unit of time passes
//When the time runs, the time will be compared to
//the animals feeding time. When that feed time passes,
//The animal gains weight and the new weight is shown.
int choice;
cout << "1) Simulate Days" << endl;
cout << "2) Display Animals" << endl;
cout << "3) Exit" << endl;
//Added code to create a Barn object
Barn myBarn;
cout << "Choose an option: n";
cout << "Enter 1, 2 or 3: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "nChoice 1"; //Here we will run feedAnimal
//Call the feedAnimal() method
myBarn.feedAnimal();
break;
case 2:
cout << "nChoice 2";
//Call the display() method
myBarn.display();
break;//Here we will display all the animals
case 3:
cout << "nChoice 3"; break;//Here we will exit the program
default:
cout << "nNot 1, 2 or 3"; break;
}
}
相关文章:
- 如何在 c++ 的类中递归调用函数方法?
- 通过构造函数方法输出的类到类类型转换是 5500 为什么不是 5555
- 在 *.cpp 文件中实现的 c++ 函数/方法永远不会内联扩展吗?
- 如果子类中没有构造函数方法,则错误"no matching function for call to 'LGame::LGame(String&)'"
- 指向类中函数方法的指针不起作用
- 如何发送通过绑定到函数/方法创建的函数对象?
- 编译错误,未创建函数/方法! 对于 brms 模型
- 对静态重载(类)函数/方法的调用是不明确的
- 复制构造函数方法的用法
- C/C++ 函数/方法与 Java 的公开
- std ::函数方法参考无法将dword投入到dword64
- 链表的析构函数方法
- 为什么在类构造函数方法中,std::string 参数在调试时显示不同的结果?
- 我的构造函数方法不接受参数(DirectX / Windows)
- 当函数/方法应该要求 * 或 &
- 带有常量构造函数参数的C++变量构造函数方法
- 使用模板递归检查函数方法是否存在
- 如何通过析构函数方法删除对象(类)
- 为什么在 Java 和 C++ 中不允许隐藏虚拟函数/方法?
- 在构造函数方法中返回一个子类