c++ - 如何使用 getArray() 将对象添加到对象的动态数组中;
问题描述
我使用 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
解决方案
推荐阅读
- c# - 使用包含点的库在跨平台(ubuntu/windows)上调用 p/invoke
- css - 类似背景附件的效果:固定但带有 img 块和位置
- .net - 如何在 Windows 窗体应用程序中使用 AD 对话框“选择用户或组”?
- data-structures - 字符串的循环缓冲区
- angular - 在高图中设置工具提示的持续时间
- php - 如何在 Centos 7 中将 php-ratchet 与 php-zeromq 连接起来
- javascript - 如何使用 Javascript/JQuery 将 iFrame 中创建的模式附加到具有不同域的父窗口
- elasticsearch - Elasticsearch“查询字符串中的AND”与“default_operator AND”
- javascript - 如何以步骤形式进行验证?
- javascript - 在单击jquery上绑定两个事件