0

I have a struct like this:

/* Renderable definition */
    struct Renderable
    {
        Renderable(VertexBufferPtr vertexBuffer, const Mat4& wvpMatrix, const Mat4& worldMatrix, const Vec4& diffuseColor, const float specularFactor) : 
                    mVertexBuffer(vertexBuffer), mTransform(wvpMatrix, worldMatrix), mMaterial(diffuseColor, specularFactor)
        {
        }

        /* Transform definition */
        struct Transform
        {
            Transform(const Mat4& wvpMatrix, const Mat4& worldMatrix) : mWVPMatrix(wvpMatrix), mWorldMatrix(worldMatrix)
            {
            }

            const Mat4 mWVPMatrix;
            const Mat4 mWorldMatrix; 
        };

        /* Material definition */
        struct Material
        {
            Material(const Vec4& diffuseColor, const float specularFactor) : mDiffuseColor(diffuseColor), mSpecularFactor(specularFactor)
            {
            }

            const Vec4 mDiffuseColor;
            const float mSpecularFactor;
        };


        const VertexBufferPtr mVertexBuffer;
        const Transform mTransform;
        const Material mMaterial;
    };

    /* RenderQueue definition */
    typedef std::vector<Renderable> RenderQueue;

When I try to use it in my code like this;

RenderQueue CreateRenderQueue(const Scene* scene);

....

RenderQueue renderQueue(CreateRenderQueue(activeScene));  

I get the follow compile error:

c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(2514): error C2582: 'operator =' function is unavailable in 'Renderable'

After some digging I came to realise it was because I had not defined the assignment operator and the copy-constructor. I then did so, and voilah! it compiled...

....My question is however, why is the assignment operator and the copy constructor NOT implicitly generated by the compiler? (vs2010) I did not define them, so surely they would be generated?

Thanks

KaiserJohaan
  • 9,028
  • 20
  • 112
  • 199

2 Answers2

5

You have constant class member objects, so you cannot assign those. A copy constructor should be generated, though.

alehresmann
  • 226
  • 3
  • 6
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
0

See the answer to this question, which states the conditions for an implicit default constructor (emphasis mine):

If you do not define a constructor, the compiler will define a default constructor for you

You defined a constructor, hence the implicit default one is not available anymore, it's up to you to define one.

In fact, a few lines below the same answer gives the same rule for all of the essential methods of classes:

If no destructor/copy Constructor/Assignment operator is defined the compiler builds one of those for you

Community
  • 1
  • 1
didierc
  • 14,572
  • 3
  • 32
  • 52