繁体   English   中英

将指向对象数组的指针传递给函数

[英]Passing pointer to array of objects into function

当我运行此代码时,它崩溃了,没有任何错误。 我已经尝试了我所知道的一切并完成了搜索,但是我可以弄清楚这一点。 它的构建和运行良好,一直到FleetCapacity中的cout,关于那条线的事情使代码崩溃了。 当我注释掉该行时,代码运行良好,所以我不确定为什么该行代码导致我的程序崩溃并烧毁。

#include <iostream>
#include <string>
using namespace std;

class Ship{
private:
    string shipName;
    string shipYear;
public:
    Ship(string sN,string sY){shipName=sN; shipYear=sY;}
    virtual void printInfo();
    void setShipName(string);
    virtual string getShipName();
    void setShipYear(string);
    string getShipYear();
};

class CruiseShip:public Ship{
private:
    int maxPass;
    int maxCrew;
public:
    CruiseShip(string sN,string sY, int mP,int mC):Ship(sN,sY){setShipName(sN);maxPass=mP; maxCrew=mC; }
    void printInfo();
    void setMaxPass(int);
    int getMaxPass();
    void setMaxCrew(int);
    int getMaxCrew();
};

class CargoShip:public Ship{
private:
    int cargoCap;
public:
    CargoShip(string sN,string sY,int cC):Ship(sN,sY){setShipName(sN);cargoCap=cC;}
    void printInfo();
    void setCargoCap(int);
    int getCargoCap();

};


void Ship::setShipName(string sN){shipName=sN;}
string Ship::getShipName(){return shipName;}
void Ship::setShipYear(string sN){shipYear=sN;}
string Ship::getShipYear(){return shipYear;}
void Ship::printInfo(){
cout<<"The ships name is "<<shipName<<endl;
cout<<"The ships year is "<<shipYear<<endl;
}

void CruiseShip::printInfo(){
cout<<"The ships name is "<<getShipName()<<endl;
cout<<"The ships maximum passangers is "<<maxPass<<endl;
}
void CruiseShip::setMaxPass(int mP){maxPass=mP;}
int CruiseShip::getMaxPass(){return maxPass;}
void CruiseShip::setMaxCrew(int mC){maxCrew=mC;}
int CruiseShip::getMaxCrew(){return maxCrew;}

void CargoShip::printInfo(){
cout<<"The ships name is "<<getShipName()<<endl;
cout<<"The ships cargo capacity is "<<cargoCap<<endl;
}
int CargoShip::getCargoCap(){return cargoCap;}
void CargoShip::setCargoCap(int cC){cargoCap=cC;}




void fleetCapacity(Ship** s ,int e){

    cout << "Name of ship: " << s[e]->getShipName() << endl;


}

int main()
{
    const int NUMSHIPS = 3;
    //int aSize = NUMSHIPS;
    // array of ship pointers initialized with addresses of dynamically allocated class objects.
    Ship *ships[NUMSHIPS] = {
                                         new Ship("The Dinghy Berry", "1982"),
                                         new CruiseShip("Disney Adventure Tours"," ",500,100),
                                         new CargoShip("The Sea Trucker"," ", 50)

                                       };

    for (int i = 0; i < NUMSHIPS; i++ )
    {
        ships[i]->printInfo();
    }

   cout << "The entire fleet capacity is: ";
   fleetCapacity(ships, NUMSHIPS);
    cout << " tons." << endl;
    return 0;
}

您正在呼叫fleetCapacity(ships, NUMSHIPS); 然后在函数中访问s[e]ships[NUMSHIPS] )。 有效索引是0NUMSHIPS-1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM