
Why is it asking me to overload the operator = ? I have previously iterated through a std::list and I did not have such problems.
class Grup : public Shape {
private:
std::vector<Shape*> continut;
public:
static const std::string identifier;
Grup();
~Grup();
void add(Shape *shape);
void remove(Shape *shape);
void output(std::ostream &outs) const;
void readFrom(std::istream &ins);
void moveBy(int x, int y);
friend std::ostream &operator<<(std::ostream &outs, const Grup &grup);
};
std::ostream &operator<<(std::ostream &outs, const Grup &grup)
{
std::vector<Shape*>::iterator it;
outs << "Grupul este format din: " << std::endl;
for (it = continut.begin(); it != continut.end(); it++)
{
}
return outs;
}
Error: "No viable overload '='. "
(After magnification of the screenshot) grup is being passed in as a const, so begin() will return a const_iterator which cannot be assigned to an iterator.
Change declaration of it to:
std::vector<Shape*>::const_iterator it;
Note In C++11 you can use auto to instruct the compiler to deduce the type:
for (auto it = grup.continut.begin(); it != grup.continut.end(); it++)
{
outs << **s << std::endl;
}
Other alternatives in C++11 are the range-based for loop:
for (auto& shape: grub.continut)
{
outs << *s << std::endl;
}
or std::for_each() with a lambda:
std::for_each(grub.continut.begin(),
grub.continut.end(),
[&](Shape* s) { outs << *s << std::endl; });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With