有人能够发现我使用这个c ++代码做错了什么吗?

时间:2020-12-23 19:24:26

So I'm breaking my head here. I've been reading up and down and I just can't figure out why my program crashes once I reach the setLoan function inside of the for loop in main. AM I missing something, or did I implement the pointer wrong? Thanks in advanced.

所以我在这里打破了我的头脑。我一直在阅读和阅读,我只是无法弄清楚为什么我的程序崩溃,一旦我到达main中的for循环内的setLoan函数。 AM我错过了什么,或者我是否实现了错误的指针?提前致谢。

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

//Vehicle Class
class Vehicle {
public:
    Vehicle();
    void setPrice(double a);
    void setMpg(int a);
    double getPrice() const;
    int getMpg() const;
    void printVehicle() const;
    Vehicle(double price, int mpg);
private:
    double price;
    int mpg;
};

//Loan Class
class Loan {
public:
    void setBank(string a);
    void setLoan(double a);
    string getBank() const;
    double getLoan() const;
    void printLoan() const;
    Loan(string bank = "", double loan = 0);
private:
    string bank;
    double loan;
};

//Car Class
class Car : public Vehicle {
public:
    Car(double price = 0, int mpg = 0, string bank = "", double loan = 0, string name = "", int element = 0);
    void setName(string a);
    void setLoan(string b, double l, int element);
    string getName() const;
    void printFull() const;
    void setNbrOfLoans(int a);
    ~Car();
private:
    string name;
    Loan* pLoan;
    int nbrOfLoans;
};

//Main
int main() {
    Car car1(24800, 22, "Citi", 21600, "Mustang", 1);
    Car car2;
    Car* pCar1 = &car1;
    Car* pCar2 = &car2;
    cout << "Enter the price of the car: ";
    double price;
    cin >> price;
    pCar2->setPrice(price);
    cout << "Enter the mpg: ";
    int mpg;
    cin >> mpg;
    pCar2->setMpg(mpg);
    cout << "Enter the name of the car: ";
    string name;
    cin >> name;
    pCar2->setName(name);
    string bank;
    double loan;
    int index;
    cout << "Enter the amount of loans you obtained: ";
    cin >> index;
    pCar2->setNbrOfLoans(index);
    for (int i = 0; i < index; i++)
    {
        cout << "Enter the name of bank " << i + 1 << ": ";
        cin >> bank;
        cout << "Enter the amount of loan " << i + 1 << ": ";
        cin >> loan;
        pCar2->setLoan(bank, loan, i);
    }
    cout << endl;
    pCar1->printFull();
    pCar2->printFull();
    return 0;
}
////////////////////////////////////////////////////////////////////////////////////////
//Vehicle class function definitions
////////////////////////////////////////////////////////////////////////////////////////
Vehicle::Vehicle() {
    price = 0;
    mpg = 0;
}

Vehicle::Vehicle(double price, int mpg) {
    price = price;
    mpg = mpg;
}

void Vehicle::setPrice(double a) {
    price = a;
}

void Vehicle::setMpg(int a) {
    mpg = a;
}

double Vehicle::getPrice() const {
    return price;
}

int Vehicle::getMpg() const {
    return mpg;
}

void Vehicle::printVehicle() const {
    cout << "Price: " << price << endl;
    cout << "MPG: " << mpg << endl;
}

////////////////////////////////////////////////////////////////////////////////////////
//Loan Class function definitions
///////////////////////////////////////////////////////////////////////////////////////

Loan::Loan(string bank, double loan) {
    bank = bank;
    loan = loan;
}

void Loan::setBank(string a) {
    bank = a;
}

void Loan::setLoan(double a) {
    loan = a;
}

string Loan::getBank() const {
    return bank;
}

double Loan::getLoan() const {
    return loan;
}
void Loan::printLoan() const {
    cout << "Bank: " << bank << endl;
    cout << "Loan: " << loan << endl;
}

////////////////////////////////////////////////////////////////////////////////////
//Car Class function definitions
////////////////////////////////////////////////////////////////////////////////////
Car::Car(double price, int mpg, string bank, double loan, string name, int element) : Vehicle(price, mpg)
{
    nbrOfLoans = element;
    Car::name = name;
    pLoan = new Loan[nbrOfLoans];
}


void Car::setName(string a) {
    name = a;
}

void Car::setLoan(string b, double l, int element) {
    pLoan[element].setBank(b);
    cout << " ";
    pLoan[element].setLoan(l);
}

string Car::getName() const {
    return name;
}

void Car::setNbrOfLoans(int a) {
    nbrOfLoans = a;
}

void Car::printFull() const {
    cout << endl << "Name: " << name << endl;
    printVehicle();
    for (int i = 0; i < nbrOfLoans; i++)
    {
        cout << pLoan[i].getBank();
        cout << endl;
        cout << pLoan[i].getLoan();
        cout << endl;
    }
}

Car::~Car() {
    delete[] pLoan;
}

1 个解决方案

#1


0  

What you are doing wrong is you are trying to access a dynamic array of size zero when you call Car::setLoan for pCar2.

你做错了是你在为pCar2调用Car :: setLoan时尝试访问大小为零的动态数组。

Car car2;//the nbrOfLoans member is 0 and pLoan is not pointing to anywhere
....
Car* pCar2 = &car2;//the nbrOfLoans member is 0 and pLoan is not pointing to anywhere
...
Car::Car(double price, int mpg, string bank, double loan, string name,    int element) : Vehicle(price, mpg)
{
   ...
   pLoan = new Loan[nbrOfLoans];//doesn't create anything if nbrOfLoans is 0, which is the default
}

When you first try to allocate memory for pLoan in the constructor of the second car(car2), the size of nbrOfLoans is 0, so your pointer is not pointing to anything. Later you tried to access it by calling pCar2->setLoan(bank, loan, i);.

当您第一次尝试在第二辆汽车(car2)的构造函数中为pLoan分配内存时,nbrOfLoans的大小为0,因此您的指针不指向任何内容。之后您尝试通过调用pCar2-> setLoan(bank,loan,i);来访问它。

It would be better if you could use std::vector otherwise you need to create/resize your dynamic array after you know the size that it should hold,i.e after getting the value of nbrOfLoans at Car::setNbrOfLoans.

如果你可以使用std :: vector会更好,否则你需要在知道它应该保持的大小后创建/调整动态数组的大小,即在获取car :: setNbrOfLoans中的nbrOfLoans值之后。

void Car::setNbrOfLoans(int a) {
   nbrOfLoans = a;

   if(pLoan!=NULL)
     delete[] pLoan;

   pLoan = new Loan[nbrOfLoans];
}

#1


0  

What you are doing wrong is you are trying to access a dynamic array of size zero when you call Car::setLoan for pCar2.

你做错了是你在为pCar2调用Car :: setLoan时尝试访问大小为零的动态数组。

Car car2;//the nbrOfLoans member is 0 and pLoan is not pointing to anywhere
....
Car* pCar2 = &car2;//the nbrOfLoans member is 0 and pLoan is not pointing to anywhere
...
Car::Car(double price, int mpg, string bank, double loan, string name,    int element) : Vehicle(price, mpg)
{
   ...
   pLoan = new Loan[nbrOfLoans];//doesn't create anything if nbrOfLoans is 0, which is the default
}

When you first try to allocate memory for pLoan in the constructor of the second car(car2), the size of nbrOfLoans is 0, so your pointer is not pointing to anything. Later you tried to access it by calling pCar2->setLoan(bank, loan, i);.

当您第一次尝试在第二辆汽车(car2)的构造函数中为pLoan分配内存时,nbrOfLoans的大小为0,因此您的指针不指向任何内容。之后您尝试通过调用pCar2-> setLoan(bank,loan,i);来访问它。

It would be better if you could use std::vector otherwise you need to create/resize your dynamic array after you know the size that it should hold,i.e after getting the value of nbrOfLoans at Car::setNbrOfLoans.

如果你可以使用std :: vector会更好,否则你需要在知道它应该保持的大小后创建/调整动态数组的大小,即在获取car :: setNbrOfLoans中的nbrOfLoans值之后。

void Car::setNbrOfLoans(int a) {
   nbrOfLoans = a;

   if(pLoan!=NULL)
     delete[] pLoan;

   pLoan = new Loan[nbrOfLoans];
}