c ++显示单维数组

时间:2014-04-28 03:24:44

标签: c++ arrays

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

      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;

               };       
                  };

print()函数在类Horse:

中看起来像这样
void print(){
               cout << "Horse Name: " << name << endl;
               cout << "Horse Lane: " << lane << endl;
               cout << "Horse Time: " << time << endl;

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

1 个答案:

答案 0 :(得分:2)

更改

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

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

Horse::print()返回void,因此无需打印。