Store custom Component into Vector

Hello, I'm trying to store a custom class that I made (called Circle), which inherits from Component, into a vector so I can draw a collection of circles all over the screen and erase them. I want to add the with push_back() and erase with pop_back. I'm only overriding the paint() method. I'm getting a compile every when I try to use push_back though and I can't seem to figure out how to do it.

Here's the error:

Error    2    error C2248: 'juce::Component::Component' : cannot access private member declared in class 'juce::Component' (..\..\Source\MainComponent.cpp) 

 

Here's the class:

 

class Circle    : public Component 
{ 
public: 
Circle(const float x, const float y, const float radius, const Colour colour); 
void paint (Graphics&); 

private: 
    float m_x; 
    float m_y; 
    float m_radius; 
    Colour m_colour; 
    float m_diameter; 
}; 

Circle::Circle(const float x, const float y, const float radius, const Colour colour) 
    :m_x{ x }, m_y{ y }, m_radius{ radius }, m_colour{ colour } 
{ 
    m_diameter = m_radius * 2;
} 

void Circle::paint(Graphics& g) 
{
    g.setColour(m_colour); 
    g.fillEllipse(m_x, m_y, m_diameter, m_diameter); 
}

 

And I'm just trying to use it something like this:

vector<Circle> circles;
circles.push_back(Circle{ x, y, 10.0f, red });

 

I'm sure there's a much better way of doing this, but I'm new to JUCE and this is similar to the way I did it in Cinder. If anyone knows how to fix this, or even a better way of doing this I would be really grateful. Idk why I can't get this to work. I don't know if I should even be inheriting from Component.

 

 

Actually, I just didn't inheret from anything and I got it working. I guess that'll work for what I'm wanting to do. :)

I'm still curious if you can create and store components in an array though. I'd like to create an array of circles then somehow move those circles around by clicking + dragging them. 

Well.... just try this to call the constructor instead of loading directly the private members, it should work better :-)

circles.push_back(Circle ( x, y, 10.0f, red ) );

But anyway you should consider using Juce's Array or OwnedArray instead of stdlib members. Take time having a look at the complete Juce object model, you'll find a lot of useful things that will simplify your life ;-)

 

/Phil

Thanks for replying man. I'll have to check out JUCE's arrays and other data structures. This is just practice anyways. I'm trying to learn as much about JUCE as I can and I thought this would be a good exercise. 

What do you know, Getting Started With JUCE has 2 sections on storing components in the JUCE Array and OwnedArray classes. Man, I feel dumb for not noticing that. Now I should be able to figure that out. :) That's in chapter 3 if anyone else needs this.

Component is NOT copyable (and AFAIK not movable)?

No - how could it be copyable!? If you copy it, what would happen to its child components or parent component? What about all the listeners that are attached? etc etc!

Hey Jules, it was not a question. It was just a suggestion to the OP why using a vector of Components is not a good idea. Of course it can not be copied :wink:

Sure :)

I was just confirming why that's the case, for anyone who might have been reading and not seeing why that is.

1 Like

i had a feeling that's what it was. Thanks for replying everyone. I'm still figuring out how components work. I love how helpful this forum is. Sorry for all the noob questions. I try to only ask if I can't find the answer.