Problems with array of custom class


#1

Hello,

I'm trying to make an array with my own class as the ElementType.

I've read the docs for array, and it says "it must have a copy constructor and assignment operator". I think that's what I've done, but I'm getting the following errors...


error: ‘OtherClass::OtherClass(const OtherClass&)’ cannot be overloaded
error: with ‘OtherClass::OtherClass(const OtherClass&)’
error: ‘OtherClass& OtherClass::operator=(const OtherClass&)’ cannot be overloaded
error: with ‘OtherClass& OtherClass::operator=(const OtherClass&)’

Am I doing something silly? I tried looking at the Rectangle class for inspiration as it can be used as Array<Rectangle>


//---------------------------------
class OtherClass :     public Component
{
public:
    
    OtherClass()            {}
    ~OtherClass()            {}
    OtherClass& operator= (const OtherClass& other) noexcept
    {
        return *this;
    }
    OtherClass (const OtherClass& other) noexcept
    {
    }
private:
    void resized()            {}
    void paint(Graphics& g)    {}
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OtherClass)
    
};


//---------------------------------
class BaseClass :     public Component
{
public:
    
    BaseClass()                
    {
        OtherClass oc;
        myArray.add(oc);
    }
    ~BaseClass()            {}
private:
    void resized()            {}
    void paint(Graphics& g)    {}
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BaseClass)
    
    Array<OtherClass> myArray;
    
};

#2

You can't use Component as a base class for a copy-by-value object, they're not designed to be assignable.

And if you're struggling with value types, you should do some C++ tutorials about it - this is fundamental language stuff, and you need to understand it!


#3

Thanks Jules. I seem to have overlooked the OwnedArray..


//---------------------------------
class OtherClass :     public Component
{
public:
    
    OtherClass()            
        {value = 10;}
    ~OtherClass()            {}
    void printValue()
        {DBG(String(value));}
private:
    void resized()            {}
    void paint(Graphics& g)    {}
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OtherClass)
    
    int value;
    
};


//---------------------------------
class BaseClass :     public Component
{
public:
    
    BaseClass()                
    {
        myArray.add(new OtherClass());
        myArray[0]->printValue();
    }
    ~BaseClass()            {}
private:
    void resized()            {}
    void paint(Graphics& g)    {}
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BaseClass)
    
    OwnedArray<OtherClass> myArray;
    
};

#4

I know this topic is old, but don't want to open an own thread for this silly problem....

I have problems using the OwnedArray in an audio plugin. It works fine until the resized() function is called. The array is empty there. I followed the tutorial about OwnedArrays in the book "Getting started..." and it worked well. Now, when I've implemented the same in my audio plugin, this problem occurs.

Any ideas why the array is seen empty in the resized() function?


#5

Because you are doing something wrong. :wink: but seriously, without seeing the code it’s close to impossible to comment. My first guess is ‘scope’.