Reputation: 45
I overloaded operator = for the class TextureImage in the class but the compiler keep saying no viable operator "=" .
TextureImage& operator=(TextureImage i){
this->x = i.getX();
this->y = i.getY();
this->n = i.getN();
this->data = i.getData();
return *this;
}
If I add const
to the function the compiler says I can't assign to non-static data member within const member function.
So how to overloaded operator = here
Upvotes: 0
Views: 81
Reputation: 1
You certainly don't want to have the assignment operator overloaded as const
member function. If you insist, make all the member variables mutable
.
The canonical form looks like
TextureImage& operator=(const TextureImage& i) {
x = i.x;
y = i.y;
n = i.n;
data = i.data;
return *this;
}
Note you don't need to use the getters there.
If there's nothing more to do there, you don't need to implement your own assignment operator, since the compiler already generates that code automatically or on demand
TextureImage& operator=(const TextureImage& i) = default;
Though I suspect data
needs some more special handling, since it sounds that is kind of array or pointer member variable.
In this case I would recommend you use a std::array
or std::vector
as data member and stick with the default implementation.
Upvotes: 2