I have been having this problem all day with my C++ lab. As far as I can tell, I have everything working, except for this one clause that my professor has stipulated in our assignment:
The order of class declarations in your source file is InventorySystem, InventoryItem, Product, eProduct. Since InventorySystem contains InventoryItem array of pointers you must use forward declaration on InventoryItem
So InventoryItem -> Product -> eProduct relate to each other in a derived heriarchy, and our assignment is to write that heirarchy combined with an InventorySystem class to manage an array of pointers to eProduct objects.
Unfortunately, all of my trolling of StackOverflow posts have led me to the conclusion that what is asked of me is impossible. As I understand forward declaration, "it is really only useful to inform the complier that the class exists" and anything contextual concerning the structure or the definition of the code would not work with forward declaration-- something that seems to directly conflict with the other requirements of my lab, as InventorySystem has a method required called BuildInventory that parses a formatted textfile and dynamically allocates an array of pointers to eProduct objects. Does that not require a constructor of the "forward declared" object?
I really, really hope that I'm just being a newbie at C++ and that I'm massively misinformed, since this issue has been driving me nuts all day.
Thanks in advance for your help.
PS: sorry for the weird casing of function names and variable, it is how my professor wrote the casing in the our assignment, and I thought it safer just to roll with what he established rather.
//Format for text file is Name;Quantity;Price;Condition
void BuildInventory()
{
ifstream fin ("in.txt");
string name="";
string Buffer = "";
int quantity = 0;
double price = 0.0;
int temp = 0;
if (!fin) {
cout << "ERROR: Failed to open input file\n";
exit(-1);
}
while ( getline (fin, Buffer, ';') ) {
string condChar = "";
Condition condition = NEW;
name = Buffer;
getline (fin, Buffer, ';');
quantity = atol (Buffer.c_str ( ) );
getline (fin, Buffer, ';');
price = atof (Buffer.c_str( ) );
getline (fin, Buffer, '\n') ;
condChar = Buffer.c_str();
if(condChar.compare("R") == 0)
condition = REFURBISHED;
else if(condChar.compare("U") == 0)
condition = USED;
else if(condChar.compare("D") == 0)
condition = DEFECTIVE;
ep = new eProduct(name, quantity, price , condition);
ItemList[ItemCount] =ep;
++ItemCount;
fin.ignore(1, '\n');
}
fin.close();
Sort();
}
Below are the constructors for the hierarchy of objects that the array of pointers dynamically allocated by InventorySystem have to point to (all point to eProducts)
//Constructor of eProduct
eProduct(string Name, int Quantity, double Price, Condition condition)
:Product(Name, Quantity, Price)
{
this -> condition = condition;
}
//Constructor of Product
Product():ProductID(0), Price(0.0){}
Product(string Name, int Quantity, double Price)
:InventoryItem(Name, Quantity)
{
this -> Price = Price;
this -> ProductID = generateProductID();
}
//Constructor of InventoryItem
InventoryItem(std::string Name, int Quantity)
{
this -> Name = Name;
this -> Quantity = Quantity;
}