assignment operator trouble.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • iapetus7
    New Member
    • Mar 2007
    • 3

    assignment operator trouble.

    So i have this class CVector which looks a little like this

    class CVector
    {
    const CVector& operator=(const CVector& right)
    {
    X = right.X;
    Y = right.Y;
    Z = right.Z;

    return *this;
    }

    const CVector& operator |=(const float length) const
    {
    return *this = ( *this | length );
    }

    float X, Y, Z;
    }

    and im getting the following error

    c:\nebula studios\project s\game\game\vec tor.h(121) : error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const CVector' (or there is no acceptable conversion)
    c:\nebula studios\project s\game\game\vec tor.h(13): could be 'const CVector &CVector::opera tor =(const CVector &)'
    while trying to match the argument list '(const CVector, const CVector)'

    thrown by the line
    return *this = ( *this | length );

    i dont know if its the line itself or the assignment operator, from wat i understand it should work.
  • stroken
    New Member
    • Mar 2007
    • 24

    #2
    Have you declared operator |, and how?

    Comment

    • iapetus7
      New Member
      • Mar 2007
      • 3

      #3
      ya, here is the code
      const float operator !() const
      {
      return sqrtf(X*X + Y*Y + Z*Z);
      }

      const CVector operator |(const float length) const
      {
      return *this * (length/ !(*this));
      }

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        you need an operator*(), also you declare
        Code:
        const CVector& operator |=(const float length)  const
        {
        return *this = ( *this | length );
        }
        as a constant member function yet you change *this, should it be
        Code:
        const CVector& operator |=(const float length) 
        {
        return *this = ( *this | length );
        }

        Comment

        • iapetus7
          New Member
          • Mar 2007
          • 3

          #5
          hahaha omg that was it??? lol, i feel like such an idoit now. Thanks man. (ya it was the const thing. the * operator was defined, thought i had included it in the first post. sorry.

          Comment

          Working...