這個程式是一個 C 專案,它使用谷倉和動物的類來創建一個動物谷倉。這些動物通過它們的名字、型別和重量來識別。在指定的時間,動物會進食并增加體重。用戶可以選擇要經過多少天。大部分代碼已經撰寫完畢,現在是讓 main 與類函式一起作業的問題。當我嘗試在 main 中執行函式時,我收到錯誤代碼“main.cpp:117:28: error: 'class Animal' has no member named 'display'”,關于以下行:cout << barn[ 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";
}
}
}
}
}
}
uj5u.com熱心網友回復:
不是撰寫一個完全獨立的函式,而是運算子 << 可以被多載的方式:
#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;
}
uj5u.com熱心網友回復:
在類內部,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();
}
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/366064.html
