我使用for循环为对象分配适当的值,以便完全准备好将对象添加到动态数组中(需要使用对象的动态数组)
我的大脑无法真正弄清楚我如何使用从getCarArray();
函数返回的地址
我尝试创建指向对象的指针,该对象已分配给tempDealer.getCarArray();
(我通过调试了解到,这些术语不能以这种方式分配)
如何将tempCar对象添加到动态数组?
添加汽车功能
{
Dealer tempDealer;
Car tempCar;
Car *carArr; //here is the pointer that I attempted to assign to the address from
//tempDealer.getCarArray();
int newCar;
std::string newVIN;
std::string newMake;
std::string newModel;
int newYear;
double newPrice;
std::cout<<"Which dealer would you like to add the car to? "<<std::endl;
displayDealers(vec);
std::cin>>newCar;
int newCarQuantity;
std::cout<<"How many cars do you want to add?"<<std::endl;
std::cin>>newCarQuantity;
for (int i = 0; i < newCarQuantity; ++i)
{
std::cout<<"What are the specs of the this car?"<<std::endl;
std::cin.ignore();
std::cout<<"Enter new VIN:";
getline(std::cin, newVIN);
std::cout<<"Enter new Make:";
getline(std::cin, newMake);
std::cout<<"Enter new Model:";
getline(std::cin, newModel);
std::cout<<"Enter new Year:";
std::cin>>newYear;
std::cout<<"Enter new Price:";
std::cin>>newPrice;
tempCar.setVIN(newVIN);
tempCar.setMake(newMake);
tempCar.setModel(newModel);
tempCar.setYear(newYear);
tempCar.setPrice(newPrice);
//as of this point, tempCar is fully created
//now its a matter of storing tempCar in the dynamic array that was created, but getCarArray()
//can only return the address,
}
}
汽车和经销商类
#define PA3_DEALER_H
#include <iostream>
#include <fstream>
class Car {
private:
std::string VIN;
std::string MAKE;
std::string MODEL;
int YEAR;
double price;
public:
Car();
Car(std::string _VIN, std::string _MAKE, std::string _MODEL, int year, double price);
std::string getVIN(){return VIN;}
std::string getMake(){return MAKE;}
std::string getModel(){return MODEL;}
int getYear() const{return YEAR;}
double getPrice() const{return price;}
void setVIN(std::string _VIN);
void setMake(std::string _MAKE);
void setModel(std::string _MODEL);
void setYear(int _year);
void setPrice(double _price);
friend std::ostream& operator << (std::ostream & out, Car _car);
};
class Dealer {
private:
std::string dealerName;
int dealerNumber;
int numberOfCars;
Car *carArrayPtr;
public:
Dealer();
Dealer( std::string _dealerName, int _dealerNumber,int _numberOfCars);
Car* getCarArray(){return carArrayPtr;}
void setDealerName (std::string _dealerName);
void setDealerNumber (int _dealerNumber);
void setNumberOfCars ( int _numberOfCars);
void setCarArray(Car *_carArrayPtr);
std::string getDealerName(){return dealerName;}
int getDealerNumber() const{return dealerNumber;}
int getNumberOfCars() const{return numberOfCars;}
friend std::ostream& operator<<(std::ostream& out, const Dealer& _dealer);
};
#endif //PA3_DEALER_H