got this assignment to write a class for sets and provide various member functions e.g add code, set union. each set has a certain number of codes. each code is a 5 charater c-string. i cant seem to get the set union (below) working.
Code:
class set
{
public:
set();
set operator+( const set& u ) const; //this operator is the set_union() function
private:
char (*codes)[string_length]; //variable codes is a pointer to an array of characters "5"
int num_codes; //the memory for the number of codes and the 5 character string
};//end
set set::operator+( const set& u ) const
{
int i, j = 0;
set union_set;
union_set.num_codes = num_codes + u.num_codes; // e.g num_codes = 2 and u.num_codes = 1, union_set.num_codes = 3
union_set.codes = new char[union_set.num_codes][string_length]; //allocate memory needed
for (i < 0; i < num_codes; i++)
{
for (j = 0; j < string_length; j++) //codes from a will go into union_set.codes e.g union_set.codes[0][2] = codes[0][2]
{
union_set.codes[i][j] = codes[i][j];
}
}
for (i = num_codes; i < union_set.num_codes; i++) //codes from a will go into union_set.codes e.g union_set.codes[2][2] = u.codes[0][2]
{
for (j = 0; j < string_length; j++)
{
union_set.codes[i][j] = u.codes[i-num_codes][j]; //u.codes[i-num_codes] means u.codes starts at 0
}
}
return union_set;
}
void main()
{
set a, b, un;
un = a + b;
}
the codes from a (codes) and b (u.codes) dont seem to get stored in union_set.
anyhelp would be grateful as assignment is due in soon, thanks