我在编译程序时出现问题,该程序显示有关“马”类的信息。尝试显示信息时出现问题。

      for(int i = 0; i < 10; i++){
              cout << ahorse[i].print() ;
              };


ahorse是Horse类的一个实例。马类看起来像这样:

class Horse{
  private:
          string name ;
          int lane;
          double time;
  public:
          Horse(string hname , int hlane , double htime){
                       name = hname ;
                       lane = hlane ;
                       time = htime;
                       };
          Horse(){
                  name = "" ;
                  lane = 0 ;
                  time = 0 ;
                  };

          void setname(string hname){
               name = hname;
               };
          void setlane(int hlane){
               lane = hlane;
               };
          void settime(double htime){
               time = htime;
               };

          string getname (){
                 return name ;
                 };
          int getlane(){
                 return lane;
                 };
          double gettime(){
                 return time;
                 };




          void print(){
               cout << "Horse Name: " << name << endl;
               cout << "Horse Lane: " << lane << endl;
               cout << "Horse Time: " << time << endl;

               };
                  };


在Horse类中,print()函数如下所示:

void print(){
               cout << "Horse Name: " << name << endl;
               cout << "Horse Lane: " << lane << endl;
               cout << "Horse Time: " << time << endl;


名称,泳道和时间是分别存储名称,泳道和时间的私有变量

最佳答案

更改

for(int i = 0; i < 10; i++){
        cout << ahorse[i].print() ;
    };




for(int i = 0; i < 10; i++){
            ahorse[i].print() ;
        };


Horse::print()返回void,因此没有要打印的内容。

09-11 12:08