can't use default assignment on constructor parameter

Hi,

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?

Thank you very much in advance!!


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
class Train{
    public:
        Train(CTrainLine &tLine, Platform start, PassengerManager &pMan);
        Train(CTrainLine &tLine, Platform prev, int travelTime, PassengerManager &pMan);
        void update();
        void travel();
        void board();
        CTrainLine getLine();
        Platform getNextPlat();
        Platform getPrevPlat();
        Platform getCurrPlat();
        string getStatus();
        void setStatus(string newstat);
        int getPop();
        int getTripTime();
        void displayTrainInfo();
        
    private:
        PassengerManager &passMan;
        PassengerList cars[3]; 
        CTrainLine &trainline;
        string status,
               location;
        Platform nextPlat, prevPlat, currPlat;
        int tripTime;
};


Train::Train(CTrainLine &tLine, Platform start, PassengerManager &pMan): trainline(tLine), passMan(pMan){
    location = start.getStation();
    trainline = tLine;
    passMan = pMan;
    currPlat = start;    
    nextPlat = tLine.getNext(start);
    tripTime = 0;
    status = "traveling";
}
32 non-static reference member `CTrainLine&PassengerManager::redLine', can't use default assignment operator
¿what's redLine?
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.
Last edited on
Thanks for the quick reply!

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;       
    }    
}
UPDATE:

I read that the solution to this is to make CTL.getSize() return a const int, so I made that change, but still getting the same error.
No, the solution is to declare the method as const so it can operate with constant objects.
int getSize() const;
Topic archived. No new replies allowed.