Feb 3, 2013 at 11:06am Feb 3, 2013 at 11:06am UTC
You have posted main.cpp twice.
Feb 3, 2013 at 12:42pm Feb 3, 2013 at 12:42pm UTC
lol, my mistake. That however is not the reason for the error :P
list.cpp
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
#include "list.h"
#include <iostream>
List::List(Item ar[], int max)
{
int i = 0;
while (i < max)
{
arr[i] = ar[i];
i++;
}
elements = i;
}
void List::AddToList(Item a)
{
if (elements < MAX)
arr[elements] = a;
++elements;
}
bool List::isempty()
{
if (elements == 0)
return true ;
else
return false ;
}
bool List::isfull()
{
if (!elements < MAX)
return true ;
else
return false ;
}
void List::visit(void (*pf) (Item &))
{
for (int i = 0;i<elements;i++)
{
pf(arr[i]);
}
}
void List::show()
{
for (int i = 0;i<elements;i++)
{
std::cout << arr[i] << ' ' ;
}
}
Last edited on Feb 3, 2013 at 12:43pm Feb 3, 2013 at 12:43pm UTC
Feb 3, 2013 at 1:00pm Feb 3, 2013 at 1:00pm UTC
You need to rethink your List constructor(s);
Your List constructor looks like this:
List(Item ar[] = 0, int max = MAX); //not good default parameters for the constructor
The implementation looks like this:
1 2 3 4 5 6 7 8 9 10
List::List(Item ar[], int max)
{
int i = 0;
while (i < max)
{
arr[i] = ar[i];
i++;
}
elements = i;
}
In main you create a List Like this:
List age2; //Uses default parameters for constructor - which will use a NULL pointer!!!!!
Now you can see why you have the problem.
Last edited on Feb 3, 2013 at 1:00pm Feb 3, 2013 at 1:00pm UTC
Feb 4, 2013 at 6:31am Feb 4, 2013 at 6:31am UTC
Wow, that wasn't so hard after all :) I overloaded the constructor so that it would work with empty lists!