Learning from a mistake that was corrected in this very forum, I am trying to pass parameters into my class constructors CORRECTLY.
For example, here in the Train class, I am passing my CTrainLine object using the address operator & so that the constructor does not make a temporary copy. Before that fix, I was having all kinds of errors I had no idea where they were coming from.
Trying not to make the same mistake twice, I copied the exact same method to pass in my PassengerManager object. However when I do this, I get the following error:
32 non-static reference member `CTrainLine&PassengerManager::redLine', can't use default assignment operator
Can someone tell me why it's giving me this error when CTrainLine passed in just fine?
You already have it in the initialization list. Line 31 above is effectively redundant which means that you can remove it without any problems.
I assume that the reason why it didn't work was that you hadn't defined: CTrainLine CTrainLine::operator= (const CTrainLine& rhs);
Since this class likely contains pointers, it will not copy the data with a default function.
If that is the case, then make sure that you also define a copy constructor, or you'll run into problems with the initialization as well.
Unfortunately, this lead to another problem in the copy constructor.
5 : passing `const CTrainLine' as `this' argument of `int CTrainLine::getSize()' discards qualifiers
So apparently because it's a constant, I can't call getSize() on it? This object is a linked list, so I need to be able to go through each node and copy the data, otherwise it won't copy correctly.
Thoughts?
Thanks again!!
1 2 3 4 5 6 7 8 9 10 11 12 13
CTrainLine::CTrainLine(const CTrainLine &CTL){
size = 0;
head = new ListNode;
ListNode * curr, *prev, *temp;
for(int n = CTL.getSize(); n > 0; n--){
temp = CTL.find(n);
curr = new ListNode;
curr->data = temp->data; //COPY CONSTRUCT
if(n != CTL.getSize())
curr->next = prev;
prev = curr;
}
}