Sign in to follow this  

problems with classes/constructors

This topic is 4039 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4039 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this