Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

missing operator= in class

Tags:

c++

std

I have the following class, here is it full prototype:

class FlowEdge{
private:
    const uint32_t from_;
    const uint32_t to_;
    const double capacity_;
    double flow_;
public:
    FlowEdge();
    FlowEdge(uint32_t from, uint32_t to, double capacity);
    uint32_t from() const;
    uint32_t to() const;
    uint32_t other(uint32_t vertex) const throw(std::invalid_argument);
    double getCapacity() const;
    double getFlow() const;
    double residualCapacityTo(uint32_t vertex) const throw(std::invalid_argument);
    void addResidualFlowTo(uint32_t vertex, double delta) throw(std::invalid_argument);
};

I use this class as std::deque element type: std::deque<FlowEdge> in another class. When I compile my project I receive an error said that my FlowEdge class have no available operator= method. This method is created by compiler by default, isn't it? What could be a problem? I haven't operator= nor in public, nor in protected section.

like image 692
karven Avatar asked Aug 26 '26 09:08

karven


1 Answers

The compiler generates an operator= for you if it is able to do so. It's not able in your case, because you have a const member in the class. Such a member cannot be assigned to, so the default copy assignment operator wouldn't be well-defined. If you want to assign objects of this class, you have to provide a custom one, implementing it to preserve the semantics you want of the const member.

Of course, the easier alternative would be to make capacity_ a non-const double. Generally, const data members are only useful in very specific situations, and they're usually more trouble than they're worth.

like image 106
Angew is no longer proud of SO Avatar answered Aug 28 '26 21:08

Angew is no longer proud of SO