3

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.

karven
  • 65
  • 6

1 Answers1

4

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.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455