So, I was building this code recently and I got some errors (seems obvious right, hold off on the sarcasm for awhile please). Anyway, the errors specifically were:
packages.cpp:586:34: error: expected primary-expression before ‘;’ token
packages.cpp:620:15: error: no match for ‘operator>>’ in ‘std::cin >> *(p + ((long unsigned int)(((long unsigned int)i) * 8ul)))’
packages.cpp:623:20: error: ‘getCharge’ was not declared in this scope
I'm pretty sure the other two errors were caused by the same bug that caused the 2nd one but I'm not really sure why it's happening. Here's the offending code:
int main()
{
// Variable Declarations
package **p = NULL;
short siNumPackages, siNumDimensions;
// Greeting
cout << endl << "Hello! Welcome to Planet Express's virtual package delivery"
<< " service." << endl;
// Input for the number of packages
cout << endl << "Input how many packages you need delivered today ";
cin >> siNumPackages;
p = new package[siNumPackages]*; // Error 2
// For every package . . .
for (short i = 0; i < siNumPackages; i++)
{
// Input number of dimensions (with error handling)
do
{
cout << endl << "Input how many dimensions the package has ";
cin >> siNumDimensions;
if ((siNumDimensions > 4) || (siNumDimensions < 2))
cout << endl << "This company is only certified to carry 2, 3 and 4 "
<< "dimensional packages. Try again! ";
} while ((siNumDimensions > 4) || (siNumDimensions < 2));
// Assign package type to element in p
switch (siNumDimensions)
{
case 2:
{
p[i] = new pak2D;
break;
}
case 3:
{
p[i] = new pak3D;
break;
}
case 4:
p[i] = new pak4D;
}
// Input packages
cin >> p[i]; // Error 2
// Get charge
getCharge(*p[i]); // Error 3
// Output package
cout << endl << "#" << i << p[i];
}
// Deallocate Memory
delete [] p;
p = NULL;
Ok, I've fixed the issue. The problem was that my input operator functions for the derived and base classes were declared virtual (and therefore, couldn't be declared as friends). As such, the correct code would have been
*p[i] >> cin;
or something similar. Anyway, I un-virtualized the overloaded functions and made them friends and now the error is gone