problems with classes/constructors

Started by
5 comments, last by maxest 17 years, 4 months ago
i've decided to write some basic to my engine. i started with implementing CVector class the header source:

class CVector2
{
    friend CVector2 operator*(float scalar, CVector2 &vector);
    friend CVector2 operator/(float scalar, CVector2 &vector);
    friend CVector2 operator*=(float scalar, CVector2 &vector);
    friend CVector2 operator/=(float scalar, CVector2 &vector);

    public:
        float x, y;

        CVector2();
        CVector2(float nx, float ny);
        CVector2(const CVector2 &vector);
        ~CVector2();

        void setVector(float nx, float ny);
        CVector2 getVector();

        void normalize();
        void setLength(float newLength);
        float getLength();
        float dotProdcut(CVector2 vector);
        CVector2 getPerpendicularVector(int index);
        CVector2 getReflectedVector(CVector2 &normal);

        static float dotProduct(CVector2 vector1, CVector2 vector2);
        static float angleBeetwenVectors(CVector2 vector1, CVector2 vector2);

        float operator!();
        float operator%(CVector2 &vector);
	CVector2 operator=(CVector2 vector);

        CVector2 operator+(CVector2 vector);
        CVector2 operator-(CVector2 vector);
        CVector2 operator*(float scalar);
        CVector2 operator/(float scalar);

        CVector2 operator+=(CVector2 vector);
        CVector2 operator-=(CVector2 vector);
        CVector2 operator*=(float scalar);
        CVector2 operator/=(float scalar);
};

CVector2 operator*(float scalar, CVector2 &vector);
CVector2 operator/(float scalar, CVector2 &vector);
CVector2 operator*=(float scalar, CVector2 &vector);
CVector2 operator/=(float scalar, CVector2 &vector);
part of implementation:

CVector2::CVector2()
{
    x = 0.0f;
    y = 0.0f;
}



CVector2::CVector2(float nx, float ny)
{
    x = nx;
    y = ny;
}



CVector2::CVector2(const CVector2 &vector)
{
    x = vector.x;
    y = vector.y;
}



CVector2::~CVector2()
{
    //
}

...

CVector2 CVector2::getVector()
{
	return *this;
}
and the final usement:

    CVector2 v1(2,3);
    CVector2 v2();
    v2 = v1.getVector();
and my GNU compiler gives me as following:

main.cpp:19: error: assignment of function `CVector2 v2()'
main.cpp:19: error: cannot convert `CVector2' to `CVector2 ()()' in assignment
when i use code

    CVector2 v1(2,3);
    CVector2 v2;
    v2 = v1.getVector();
it's ok. why..? i'm sick of this cause i totally cannot understand why it does not work. could anyone explain me that?
Advertisement
The line:

CVector2 v2();

Is actually declaring the existance of a function called v2, which takes no arguments and returns a CVector2.

The compiler cannot differentiate between a variable and a function declaration here.

2 ways around, the one you found:

CVector2 v2;

or

CVector2 v2 = CVector2();

The second one will usually not involve the construction of a temporary variable, the compiler is allowed ( and a good one will ) optimise away the temporary.
hmm... i guess i see :) thanks a lot

i'm also interested why i need such a constructor:

CVector2(const CVector2 &vector);


because the version like that:

CVector2(CVector2 vector);


does not compile. why is it like that?
Quote:Original post by Maxest
hmm... i guess i see :) thanks a lot

i'm also interested why i need such a constructor:

CVector2(const CVector2 &vector);


because the version like that:

CVector2(CVector2 vector);


does not compile. why is it like that?


Well, to pass a CVector into that copy constructor, you need to generate a copy of the one you are passing in.

With what can you generate a copy, since you are in the process of defining the copy constructor?

So you use a const reference instead.
well, i see i need to read some "strong" guidebook about C++ :)
once more - thanks for your help
Quote:Original post by Maxest
well, i see i need to read some "strong" guidebook about C++ :)
once more - thanks for your help


This should be all you need. :)
i have added it to my firefox's sheets :)
thank you very much

This topic is closed to new replies.

Advertisement