我正在用C ++编写宠物模拟器,最初是用Visual C ++编写的。过渡期很艰难,但除了以下事实外,它主要起作用了:
undefined reference to `Pet::Pet(double, double, int, int, int, std::string, ...);'
undefined reference to `Pet::eat(int)'
7 more
所以这是我的标题(Pet.h):
#ifndef __Pet__
#define __Pet__ //picked this up learning Visual C++
//not sure if good or not
#include <string>
class Pet{
public:
bool dead;
int health;
//etc
void eat(int amount);
void play(int time);
void pet(int times);
void bathe();
void buy(std::string item, int quantity);
void stats();
void hurt();
Pet(double happiness_, double hunger_, int weight_, int age_,
int cleanliness_, string name_, string type_, int food_, int money_, bool dead);
};
#endif
这是我的班级(Pet.cpp):
#include "Pet.h"
#include <string>
#include <iostream>
//not sure if this practice is discouraged or not
using namespace std;
Pet::Pet(double happiness_, double hunger_, int weight_, int age_, int cleanliness_,
string name_, string type_, int food_, int money_, bool dead_){
hapiness = happiness_;
hunger = hunger_;
age = age_;
cleanliness = cleanliness_;
name = name_;
type = type_;
dead = dead_;
health = 100;
inventory.food = food_;
inventory.money = money_;
}
void Pet::eat(int amount){
if(hunger <= 45){
cout << "You feed " << name << ".\n";
hunger += amount;
inventory.food -= amount;
return;
}
cout << "Your pet is already full!";
}
//etc
对于巨大的文字墙感到抱歉,但有很多可能是错误的。