Jump to content
  • Advertisement
Sign in to follow this  
ToastFlambe

Correct use of "this"? (now with "Normalize()")

This topic is 4836 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'm programming a Vector2 class and have this member function for normalizing a vector
Vector2 & Vector2::Normalize()
{
    return *this / this->GetLength();
}


when I try to compile, the compiler throws up this error C:\Dev-Cpp\Projects\Vector2.cpp In member function `Vector2& Vector2::Normalize()': 116 C:\Dev-Cpp\Projects\Vector2.cpp no match for 'operator/' in '*(Vector2*)this / ((Vector2*)this)->Vector2::GetLength()' note C:\Dev-Cpp\Projects\Vector2.cpp:70 candidates are: Vector2 Vector2::operator/(float&) here is the operator/ function and the GetLength function
Vector2 Vector2::operator/(float & rhs)
{
    Vector2 temp;
    temp[0] = array[0] / rhs;
    temp[1] = array[1] / rhs;
    return temp;
}


const float Vector2::GetLength() const
{
    return sqrt(array[0] * array[0] + array[1] * array[1]);
}


Now when I wrote the Normalize function, I figured "this" dereferenced is a Vector2 object and the return value of GetLength is a float, so that would satisfy the operator/ function. Am I not right or is that even the problem? [Edited by - Alt F4 on August 21, 2005 1:10:35 PM]

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
I'm not really sure about this, but maybe you need to add parenthesis around *this:


Vector2 & Vector2::Normalize()
{
return (*this) / this->GetLength();
}

Share this post


Link to post
Share on other sites
I'm not sure what wrong with it, but if it did work, you're returning a reference to a temporary variable. It won't exist after the function returns, which is bad.

Edit: Your problem is probably that operator / takes a non-const reference.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Perhaps it is that you are returning a const float and then pass that by reference. Try making operator/ take a const reference

Share this post


Link to post
Share on other sites
Take a look at this implementation:

Vector2 & Vector2::Normalize()
{
*this /= GetLength();
return *this;
}


Vector2 & Vector2::operator/=(float const & rhs)
{
array[0] /= rhs;
array[1] /= rhs;
return *this;
}

Vector2 Vector2::operator/(float const & rhs) const
{
Vector2 temp( *this );
temp /= rhs;
return temp;
}


float Vector2::GetLength() const
{
return sqrt(array[0] * array[0] + array[1] * array[1]);
}

};

Share this post


Link to post
Share on other sites
This includes a few operators that might have been overlooked including writing to a stream, negation, and multiplying a scalar by a vector.


#include <iostream>
#include <cmath>

class Vector2
{
protected:
float array[2];

public:
Vector2(float x=0.0f, float y=0.0f)
{
array[0] = x;
array[1] = y;
}

Vector2 & operator += (const Vector2 &rhs)
{
array[0] += rhs[0];
array[1] += rhs[1];
return *this;
}

Vector2 & operator -= (const Vector2 &rhs)
{
array[0] -= rhs[0];
array[1] -= rhs[1];
return *this;
}

Vector2 & operator *= (const float &rhs)
{
array[0] *= rhs;
array[1] *= rhs;
return *this;
}

Vector2 & operator /= (const float &rhs)
{
array[0] /= rhs;
array[1] /= rhs;
return *this;
}

Vector2 operator + (const Vector2 &rhs) const
{
return Vector2(*this) += rhs;
}

Vector2 operator - (const Vector2 &rhs) const
{
return Vector2(*this) -= rhs;
}

Vector2 operator * (const float &rhs) const
{
return Vector2(*this) *= rhs;
}

Vector2 operator / (const float &rhs) const
{
return Vector2(*this) /= rhs;
}

Vector2 operator - () const
{
return Vector2(-array[0], -array[1]);
}

friend Vector2 operator * (const float &lhs, const Vector2 &rhs)
{
return rhs.operator *(lhs);
}

float GetLength() const
{
return std::sqrt(array[0]*array[0]+array[1]*array[1]);
}

Vector2 & SetLength(const float &length)
{
return operator /= (GetLength()).operator *= (length);
}

Vector2 Normal() const
{
return operator / (GetLength());
}

Vector2 & Normalize()
{
return operator /= (GetLength());
}

const float & operator [] (unsigned int index) const
{
return array[index];
}

float & operator [] (unsigned int index)
{
return array[index];
}

friend std::ostream & operator << (std::ostream &stream, const Vector2 &vec)
{
return stream << vec[0] << ',' << vec[1];
}
};


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!