Catalogint maxProducts if maxProducts 1 throw invalidargu

Catalog(int maxProducts)

{

if (maxProducts < 1)

{

throw invalid_argument(\"Cannot create catalog with less than 1 product.\");

}

mP = maxProducts;

_list = new Product[mP];

}

trying to call

Catalog a(20*1000*1000);

from main but wont work because its too big.. how do i fix this code.

it has something to do with my dynamic allocation

Solution

//I gave here one of the way to solve your problem

#include <iostream>
#include <stdlib.h>
using namespace std;
class Product
{
//you have already
};
class Catalog{
long int mP;
   Product *_list;
   public:
   Catalog(int maxProducts)
{
if (maxProducts < 1)
{

//you have to define this exception already
throw invalid_argument(\"Cannot create catalog with less than 1 product.\");
}
mP = maxProducts;
_list = (Product*) realloc (_list, mP * sizeof(Product));
}
};


int main()
{
Catalog a(20*1000*1000);  
}

Catalog(int maxProducts) { if (maxProducts < 1) { throw invalid_argument(\

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site