i’m trying to write dynamically linked list of complex numbers. Basically: I have a class list that contains a structure number that contains class complex. Box in the box in the box. I have problem with referencing the complex number itself (i only have function that adds a node. My code so far:
complex.h
class complex
{
public:
float r;
float i;
};
list.h
#include "complex.h"
class list
{
public:
typedef struct number
{
complex a;
struct number *next;
}number;
number *number;
public:
void add(number* number,complex b);
list();
~list();
};
list.cpp (contains function add, doesn’t work)
void list::add(number* number, complex b)
{
number *newo=new number;
while (number->next != NULL)
{
number = number->next;
}
newo = malloc (sizeof(number));
newo->a::r = b::r;
newo->a::i = b::i;
newo->next = NULL;
number->next = newo;
}
Thanks for help 😉
These lines
should be
or even more simply you could write
Other problems you have are
1) That you allocate the number node twice, once with
newand once withmalloc. Only do it once, and usenew.2) The number seems to be an uninitialised pointer in your
listclass. This is going to crash you program. Write a constructor forlistthat initialises number to NULL.3) Thje logic of
list::addis wrong, even if number is NULL then the first thing you do isnumber->nextwhich will also crash your program.4) Names are all over the place (as others have pointed out). Try to pick good names for everything, it will help you understand your own code.