Cannot Allocate an Array of Size 0 & illegal operation on bound member function expression

Hi;

Im new to C++ and im having issues with an embedded if statement on an array of pointers to objects loop. The code is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  #include <iostream>
#include "stock.h"
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

void productReport(int ArSize, Stock * stocks[]);
const int stockArSize = 12;
const string seperate = "################################################################################################################";

int main()
{
	{
		// Array of objects
		Stock * stocks[stockArSize] =
		{
			new Stock("Girl_with_the_dragon_tatoo", "Larsson", 'e', 9780857054036, 15.95, 5),
			new Stock("The_way_of_kings", "Sanderson", 'a', 9780575102484, 16.95, 10),
			new Stock("Fools_Quest", "Hobb", 's', 9780007444229, 19.95, 2),
			new Stock("The_Name_Of_the_Wind", "Rothfuss", 's', 9780575081406, 18.55, 10),
			new Stock("Wheres_Wally", "Handford", 's', 9781406328899, 5.50, 0),
			new Stock("Java", "Schildt", 's', 9780071808552, 62.95, 4),
			new Stock("A_Feast_for_Crows", "Martin", 'e', 9780006486121, 9.99, 20),
			new Stock("C++", "Malik", 's', 9780538798082, 42.50, 0),
			new Stock("Golden_Lion", "Smith", 'a', 9780732298241, 19.0, 2),
			new Stock("The_Martian", "Weir", 'h', 9780091956141, 33.00, 6),
			new Stock("The_BFG", "Dahl", 'a', 9780141805917, 11.23, 3),
			new Stock("Skin_Game", "Butcher", 'h', 9780356500904, 24.45, 1),
		};

	
		productReport(stockArSize, stocks);

		
	}
	return 0;
}

void productReport(int ArSize, Stock * stocks[])
{
	string filename;
	cout << "Enter the name of the file you wish to write to: ";
	cin >> filename;

	ofstream report;	// Create object for output
	report.open(filename, std::ios_base::app);

	if (!report.is_open())	// Check if file failed to open
	{
		cerr << "Could not open " << filename << endl;
		exit(EXIT_FAILURE);
	}

	// Audibooks menu bar
	report << "AudioBooks:\n" << seperate << endl;
	report	<<  "ISBN"
			<< setw(20) << "Title"
			<< setw(50) << "Retail Cost"
			<< setw(40) << "Quantity in Stock\n\n";

	// Audiobooks content
	for (int i = 0; i < ArSize; i++)
	{
		if (Stock * stocks[i].bookFormat == 'a')
		{
			report << stocks[i]->getIsbn
				    << setw(20) << stocks[i]->getTitle
					<< setw(53) << "Retail Cost"
					<< setw(39) << "Quantity in Stock\n";
		}
	}
}


Im getting a few errors in the if statement:

illegal operation on bound member function expression (when trying to call getIsbn and getTitle.

cannot allocate an array of constant size 0

Any help on why i am getting these errors would be much appreciated.

Regards

stocks[i]->getIsbn()
Thanks that removed the illegal operation error, but the if statement is still giving me the same error cannot allocate an array of constant size 0
The expression in your if condition is not valid. Presumably you're trying to do something like:
if (stock[i].bookFormat == 'a')
Ahh ok, its working well now after i changed it to:

if (stocks[i]->bookFormat == 'a')


Thanks alot for your help
Topic archived. No new replies allowed.