class TrainList{
public:
struct tNode{
Train data;
tNode * next;
};
TrainList();
~TrainList();
bool isEmpty();
void add(Train P);
tNode * find(int pos);
void remove(Train P);
int getSize();
Train remove(int pos); //Removes & deletes a node
Train retrieve(int pos); //Retrieves the data of node
void displayList();
private:
tNode * head;
int size;
};
TrainList::TrainList() {
size = 0;
head = new tNode;
}
And here's the error:
trainlist.hpp(24): error C2512: 'TrainList::tNode' : no appropriate default constructor available
The bizarre thing is that I have another class in the same project that is literallyan exact copy of this one except it's for a different type of object (passenger) and that one compiles without a problem.
As a solution, I tried declaring the struct like this:
References need to be constructed explicitly (they do not have a default constructor). So since your Train class has references as members, you would need to initialize them in your default constructor.
However that's difficult/impossible to do correctly because I assume the stuff is supposed to be referencing things outside the class.
This is one of the reasons why I generally don't have reference as class members. It's rarely justifiable.
I recommend changing trainline and passMan to be pointers instead of references. Then in your default constructor initialize them to null (do not allocate with new -- that makes little/no sense). This also means you'll have to have some other kind of initialize function where you can set these members.
Or, write a copy constructor for `Train' that copies the references.
Use it to construct your nodes.
There's still the problem of your dummy header, use a dummy node for that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
class train_list{
private:
struct dummy_node{
dummy_node *next;
dummy_node(){
next = this; //or NULL, but initialize it
}
};
struct tNode: public dummy_node{
Train data;
tNode(const Train &data): data(data){}
};
dummy_node header; //I don't understand why you used a pointer here
};
whenever you need to access the `data' field, just cast the node (it's an error to want to cast the header cell).
Don't force a default constructor if it makes no sense.