当我运行此代码时,它会崩溃而没有任何错误。我已经尝试了我所知道的一切并完成了搜索,但我可以想出这个。它构建并运行良好,一直到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;
}
答案 0 :(得分:1)
您正在调用getLocation
,然后在该函数中访问private ArrayList<Double> getLocation() {
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
if(isLocationEnabled(locationManager)){
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
ArrayList<Double> values = new ArrayList<>();
double latitude = location.getLatitude();
double longitude = location.getLongitude();
values.add(latitude);
values.add(longitude);
return values;
}
else if(!isLocationEnabled(locationManager)){
Log.d("LOCATION IS NOT ENABLED", "locatoin not enabled");
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("Turn on location and click \"OK")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
getLocation();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
return null;
}
private boolean isLocationEnabled(LocationManager lm){
boolean gps = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean network = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(!gps && !network){
return false;
} else {
return true;
}
}
(fleetCapacity(ships, NUMSHIPS);
)。有效索引为s[e]
到ships[NUMSHIPS]
。