Reputation: 11
I have been working with a vector
and I receive this error:
No matching member function for call to 'push back'
Can anyone take a look at my code to detect my mistakes?
bubble_jar.h
#include "abstract_bubble.h"
#include "bubbles.h"
#include <vector>
#include "cpputils/graphics/image.h"
#ifndef BUBBLE_JAR_H
#define BUBBLE_JAR_H
class BubbleJar : public graphics::MouseEventListener,
public graphics::AnimationEventListener {
public:
~BubbleJar();
void Initialize(int max_age);
void Start();
void OnMouseEvent(const graphics::MouseEvent& event) override;
void OnAnimationStep() override;
graphics::Image* GetImageForTesting() { return &image_; }
private:
graphics::Image image_;
int max;
std::vector<std::unique_ptr<AbstractBubble>> ab;
};
#endif // BUBBLE_JAR_H
bubble_jar.cc
void BubbleJar::OnMouseEvent(const graphics::MouseEvent& event) {
std::cout << "BubbleJar got a MouseEvent" << std::endl;
// Create a BigBubble on mouse press and a SmallBubble on mouse release
// and add them to the vector.
if (event.GetMouseAction() == graphics::MouseAction::kPressed) {
BigBubble b(event.GetX(), event.GetY(), &image_);
ab.push_back(b);
b.Draw();
}
else if ( event.GetMouseAction() == graphics::MouseAction::kReleased ) {
SmallBubble s(event.GetX(), event.GetY(), &image_);
ab.push_back(s);
s.Draw();
}
image_.Flush();
}
Upvotes: 0
Views: 677
Reputation: 596287
Your std::vector
holds std::unique_ptr
smart pointers to AbstractBubble
-derived objects, but you are trying to push actual derived objects, not smart pointers to derived objects, hence the push_back()
error due to a type mismatch.
Try this instead:
void BubbleJar::OnMouseEvent(const graphics::MouseEvent& event) {
std::cout << "BubbleJar got a MouseEvent" << std::endl;
// Create a BigBubble on mouse press and a SmallBubble on mouse release
// and add them to the vector.
if (event.GetMouseAction() == graphics::MouseAction::kPressed) {
auto b = std::make_unique<BigBubble>(event.GetX(), event.GetY(), &image_);
ab.push_back(std::move(b));
b->Draw();
}
else if ( event.GetMouseAction() == graphics::MouseAction::kReleased ) {
auto s = std::make_unique<SmallBubble>(event.GetX(), event.GetY(), &image_);
ab.push_back(std::move(s));
s->Draw();
}
image_.Flush();
}
Upvotes: 2