Opertor = is not working

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
struct PID{
    
    PID operator= (PID &rhs)const {
        PID lhs ;
        lhs.processId = rhs.processId;
        std::cout<<"rhs process id is : " << lhs.processId << std::endl;
        lhs.is_Child = rhs.is_Child;
        lhs.parent = rhs.parent;
        lhs.is_Zombie = rhs.is_Zombie;
        lhs.is_Waiting = rhs.is_Waiting;

        std::swap(rhs.processId,lhs.processId);
        if(rhs.Childs.size() != 0 ){
            for(int k=0; k< sizeof(rhs.Childs);k++){
                lhs.Childs[k] = rhs.Childs[k] ;
            }
        }

        return lhs;
    }
    
    
    int processId = 0;
    bool is_Child = false;
    int parent = 0;
    std::vector<int> Childs;
    bool is_Zombie = false;
    bool is_Waiting = false;
    
}

and i have a PID that is called CPU and i am trying to put the last element in This line "std::list<PID> Queue"
and the way i do it is as so .

CPU = Queue.end();

but i keep getting No viable overloaded '='

and i am not sure why

and it only runs when i switch Queue.end() with Queue.back()

but even then it doesnt return the right value , it just returns what in The value CPU instead
Last edited on
A few significant problems

1. std::list<...>.end() does not return an object. It returns an iterator that "points" to one past the last element in the list. So, there is nothing to use as the basis of an assignment.

2. Once you figure out which iterator you are trying to copy from, you need to get the object that you want to copy. So, it will be something like:

CPU = *(iterator into Queue);

3. operator= is used to modify an object. As such, it cannot be const. The argument should be a const&, but the function must not be const. And you modify *this, not a local variable lhs.

Why do you have that operator at all?

The compiler would create for your struct a proper copy assignment operator by default.


What is the type of Queue.end()?
What is the type of Queue.back()?
You wrote the code. Therefore, you should know.

The compiler's error message shoould show the type of arguments in the call and types of known overloads of '='. That can be cryptic.


Your operator. When we write two = one; we expect the two to change and become a copy of one.

Your operator will never change the two. It is const member function. What your operator does is creation of a temporary object lhs, whose value you do set.

When we write three = two = one; we expect it to do the same as
1
2
two = one;
three = two;

Yours does not. It does not modify the two. It does return a copy of one, but your three = one; does not modify the three.
1
2
        if(rhs.Childs.size() != 0 ){
            for(int k=0; k< sizeof(rhs.Childs);k++){
sizeof is evaluated at compile time, it'll give you the sum of the sizes of the members of your object (in the case of std::vector, a couple of pointers)
to know the size of a vector you use the member function .size(),, as you did in the line prior... ¿do you even read what you wrote?
Topic archived. No new replies allowed.