0

-让我先介绍一些背景-我的任务是拍摄一个给定的情景(我的狗伙伴在后院看到一只青蛙,如果他饿了就吃它,如果不是他会玩它,如果他已经吃了两个他会放手吧。如果他看到一只猫或松鼠,他会向它吠叫,如果另一只狗他追它,如果一只土狼他会呼救,任何其他动物他都会看着它)。然后我们要让它计算给定晚上的动物数量,并将其与 Buddy 对所述动物的反应一起记录到另一个文件中。一个人将能够在记录的文件中输入一个日期,并为所述日期提取动物和互动。-

这是我目前拥有的代码:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

class animal{
public:
   animal();
   ~animal();
   virtual string interactWithBuddy()//all derived classes use this
   {
        return "Buddy ";
   } 

};
animal::animal()
{
}
class frog: public animal
{

 public:
         string interactWithBuddy()
         {
              return "Buddy \n";
         }
         static int ID()
         {
             return 1;//ID assigned to frog for randomization purposes
         }         
};
class dog: public animal
{                
   public:
         string interactWithBuddy()
         {
              return "Buddy chased the dog\n";
         }
         static int ID()
         {
             return 2;//ID assigned to dog for randomization purposes
         }

};
class cat: public animal
{
  public:
         string interactWithBuddy()
         {
              return "Buddy barked at the cat \n";
         }
         static int ID()
         {
             return 3;//ID assigned to cat for randomization purposes
         }
};
class coyote: public animal
{
  public:
         string interactWithBuddy()
         {
              return "Buddy cried for help when he seen the coyote \n";
         }
         static int ID()
         {
             return 4;//ID assigned to coyote for randomization purposes
         }
};
class squirrel: public animal
{
  public:
         string interactWithBuddy()
         {
                return "Buddy barked at the squirrel \n";
         } 
         static int ID()
         {
             return 5;//ID assigned to squirrel for randomization purposes
         }
};
class otherAnimal: public animal
{
  public:
         string interactWithBuddy()
         {
                return "Buddy watched the animal \n";
         } 
         static int ID()
         {
             return 6; //ID assigned to otherAnimal for randomization purposes
         }
};
int main ()
{
srand(time(0)); //intializes the random seed
int number;
animal * a; // pointer to animal 
  std::cout << (rand() % 6 + 1) <<std::endl;  //return random number between 1-6

 // loop to assign the random number output a proper animal ID
  if (number == frog::ID()) 
  {
        a = new frog;
        a->interactWithBuddy();

  }
  else if (number == dog::ID())
  {
       a = new dog;
       a->interactWithBuddy();
  }
  else if (number == cat::ID())
  {
       a = new cat;
       a->interactWithBuddy();
  }
  else if (number == coyote::ID())
  {
       a = new coyote;
       a->interactWithBuddy();
  }
  else if (number == squirrel::ID())
  {
     a = new squirrel;
     a->interactWithBuddy();
  }
  else if (number == otherAnimal::ID()) 
  {
      a = new otherAnimal;
      a->interactWithBuddy();
  }

return 0;
}

编译时没有错误,但是当我对输出进行代码检查时,我得到一个错误,上面写着“第 100 行:警告:'number' is used uninitialized in this function”

4

4 回答 4

3
virtual int ID() //allows declared value in subclass
{
        return ("My ID number is\n");
}

int意味着该函数返回一个整数。但它实际上返回一个字符串 ( const char *)。编译器不知道如何将const char *您返回的转换为int您应该返回的。

于 2012-08-27T02:02:47.137 回答
1

至于第二个问题,您正在从函数返回一个 cstring,但您已将函数的返回类型声明为整数。双引号“”之间的任何内容都被视为常量字符串。更改函数的返回类型或返回整数

于 2012-08-27T02:03:31.243 回答
0

您的函数被声明为返回一个整数 - 您正在返回一个以空字符结尾的字符串

相反,在基类中可能会返回一个错误类型 ID,例如

 virtual int ID()
 {
     return -1;
 }
于 2012-08-27T02:07:35.803 回答
0

对于您的第一个问题,这可能需要尝试几次。

首先,假设您只想随机选择一种动物:

srand(time(0)); //initializes the random seed
int number = rand() % 6 + 1;
animal *a;
if(number == 1)
{
  a = new frog;
}
if(number == 2)
{
  a = new dog;
}
...

a->interactWithBuddy();
delete(a); // Don't forget to delete what you create with "new".

这行得通,但是 ID 号在这里是“硬编码”的,它不使用ID()您编写的函数。如果你想使用ID(),你可以每只动物一只,看看哪一只匹配number

frog Kermit;
dog Ralph;
cat Felix;
coyote Loki;
squirrel Sparticus;

if(number == kermit.ID())
{
  kermit.interactWithBuddy();
}
if(number == Ralph.ID())
{
  Ralph.interactWithBuddy();
}
...

您必须事先拥有每只动物,因为在动物存在之前,您不能向动物索要ID()。但是有一种编码方法,ID()这样您就可以在拥有一种动物之前询问某种动物的 ID,方法是使用“ static”:

class animal{
public:
  animal();
  ~animal();
  void interactWithBuddy();
};

class frog: public animal
{
public:
  ...
  static int ID()
  {
    return 1;
  }
};

...

int main()
{
  ...
  if(number == frog::ID())
  {
    a = new frog;
  }
  ...
}

这也解决了您的第二个问题,因为您不再OD()animal.

这足够了吗?还有其他可能性。

编辑:

你忘了int number = rand() % 6 + 1;

于 2012-08-27T03:21:57.947 回答