I need to dynamically resize my array by 2 every time it fills up. It starts with a capacity of 2. This is what I've done (in main.cpp), but it gives me these errors: "warning: deleting array 'test'" and "error: incompatible types in assignment of 'ItemInfo*' to 'ItemInfo [cap]'".
ItemInfo test[cap];
//I have code here to open file, read from file, etc
if(itemCount >= cap){
cap += 2;
ItemInfo *temp;
temp = new ItemInfo[cap];
for(int i = 0; i < cap; i++){
temp[i] = test[i];
}
delete[] test; //error here
test = temp; //error here
}
This is the class (in .hpp file):
#include <iostream>
#include <iomanip>
using namespace std;
class ItemInfo {
private:
int itemId;
char description[40];
double manCost;
double sellPrice;
public:
ItemInfo() {
itemId = 0;
*description = '\0';
manCost = 0.0;
sellPrice = 0.0;
}
void setItemId(const char *num);
void setDescription(const char *cstr);
void setManCost(const char *num);
void setSellPrice(const char *num);
int getItemId();
const char *getDescription();
double getManCost();
double getSellPrice();
std::vector. If not, look around the site a bit more and you'll find dozens of examples of how to do this withnew.cap += 2is an unusual reallocation strategy, since reallocation is expensive.cap *= 2would be more common.