# Problems with array of custom class

**URL:** <https://forum.juce.com/t/problems-with-array-of-custom-class/13143>\
**Category:** General JUCE discussion\
**Created:** [July 8, 2014, 6:09pm UTC](https://forum.juce.com/t/problems-with-array-of-custom-class/13143 "2014-07-08T18:09:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mibix](https://avatars.discourse-cdn.com/v4/letter/m/b2d939/32.png) [@mibix](https://forum.juce.com/u/mibix)\
**Post date:** [July 8, 2014, 6:09pm UTC](https://forum.juce.com/t/problems-with-array-of-custom-class/13143/1 "2014-07-08T18:09:46Z")

</div>

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&nbsp;"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?&nbsp;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;
    
};
```

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [July 8, 2014, 6:17pm UTC](https://forum.juce.com/t/problems-with-array-of-custom-class/13143/2 "2014-07-08T18:17:02Z")

</div>

You can't use&nbsp;Component as a base class for a&nbsp;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!

---

<div class="post-metadata">

**Author:** ![mibix](https://avatars.discourse-cdn.com/v4/letter/m/b2d939/32.png) [@mibix](https://forum.juce.com/u/mibix)\
**Post date:** [July 8, 2014, 7:18pm UTC](https://forum.juce.com/t/problems-with-array-of-custom-class/13143/3 "2014-07-08T19:18:02Z")

</div>

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;
    
};
```

---

<div class="post-metadata">

**Author:** ![marvu](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@marvu](https://forum.juce.com/u/marvu)\
**Post date:** [September 19, 2014, 1:39pm UTC](https://forum.juce.com/t/problems-with-array-of-custom-class/13143/4 "2014-09-19T13:39:17Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![cpr2323](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/cpr2323/32/18682_2.png) [@cpr2323](https://forum.juce.com/u/cpr2323)\
**Post date:** [September 19, 2014, 4:37pm UTC](https://forum.juce.com/t/problems-with-array-of-custom-class/13143/5 "2014-09-19T16:37:11Z")

</div>

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