beginner's question concerning operator overloading.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Carter

    beginner's question concerning operator overloading.

    I am getting a recurring error with my multiply operator for a vector
    class I am working on and I am uncertain as to the exact reason why
    the operator* fails to match properly in certain circumstances yet
    seems to work properly in others. Any help would be appreciated.

    Thanks in advance,

    Carter.

    (Code follows)

    #include <iostream>

    class vector_3d
    {
    public:

    vector_3d()
    :_x(0.0), _y(0.0), _z(0.0) {}

    vector_3d(doubl e x, double y, double z)
    :_x(x), _y(y), _z(z) {}

    vector_3d(const vector_3d& v)
    :_x( v._x ), _y ( v._y ), _z( v._z ) {}

    void set_x(double x) { _x = x; }
    void set_y(double y) { _y = y; }
    void set_z(double z) { _z = z; }

    double x() const { return _x; }
    double y() const { return _y; }
    double z() const { return _z; }

    private:
    double _x, _y, _z;
    };


    vector_3d& operator*=(vect or_3d & v, double s)
    {
    v.set_x( s * v.x() );
    v.set_y( s * v.y() );
    v.set_z( s * v.z() );
    return v;
    }


    inline vector_3d& operator*(vecto r_3d & v, double s)
    {
    return (v *= s);
    }

    inline vector_3d& operator*(doubl e s, vector_3d & v)
    {
    return (v *= s);
    }

    std::ostream& operator << ( std::ostream& os, const vector_3d& v)
    {
    return os << v.x() << "," << v.y() << "," << v.z() << std::endl;
    }

    int main()
    {
    // vector_3d v(1.0,1.0,1.0);
    vector_3d v = 2.0 * vector_3d(1.0,1 .0,1.0); // this errors out
    std::cout << (2.0 * v); // this does not
    }
  • Carter

    #2
    Re: beginner's question concerning operator overloading.

    Oops forgot to post this information sorry. Here is the error message
    coming out of GCC-

    vector.cpp: In function ‘int main()’:
    vector.cpp:92: error: no match for ‘operator*’ in ‘2.0e+0 *
    vector_3d(1.0e+ 0, 1.0e+0, 1.0e+0)’
    vector.cpp:51: note: candidates are: vector_3d& operator*(vecto r_3d&,
    double)
    vector.cpp:56: note: vector_3d& operator*(doubl e,
    vector_3d&)

    Comment

    • kwikius

      #3
      Re: beginner's question concerning operator overloading.


      "Carter" <cartercheng@gm ail.comwrote in message
      news:f84635a3-9373-41dd-b235-8317cbff90c6@t1 2g2000prg.googl egroups.com...
      >I am getting a recurring error with my multiply operator for a vector
      class I am working on and I am uncertain as to the exact reason why
      the operator* fails to match properly in certain circumstances yet
      seems to work properly in others. Any help would be appreciated.
      With a A quick look I think your problems due to const non const references.
      At the error message you have created a temporary vector and the compiler
      will not allow it to be passed into a function to as a non-const reference.
      Of course changing the sig of your op functions will cause other problems.
      However generally IMO where op is some non assignment operator (e.g + , * ,
      /) you should return a copy of the vector rather than the original, and
      pass your arguments by const reference rather than reference

      V operator Op ( V const & x , V const & x);

      rather than

      V& operator Op ( V & x , V & x);

      For multiply assign you can use *= etc.

      This then mimics more closely the way the inbuilt types work and causes less
      surprises.

      regards
      Andy Little








      Comment

      • Jim Langston

        #4
        Re: beginner's question concerning operator overloading.

        Carter wrote:
        Oops forgot to post this information sorry. Here is the error message
        coming out of GCC-
        >
        vector.cpp: In function ‘int main()’:
        vector.cpp:92: error: no match for ‘operator*’ in ‘2.0e+0 *
        vector_3d(1.0e+ 0, 1.0e+0, 1.0e+0)’
        vector.cpp:51: note: candidates are: vector_3d& operator*(vecto r_3d&,
        double)
        vector.cpp:56: note: vector_3d& operator*(doubl e,
        vector_3d&)
        I may be wrong, but I belive because of const correctness.

        inline vector_3d& operator*(doubl e s, vector_3d & v)

        This isn't matching
        2.0 * vector_3d(1.0,1 .0,1.0);

        because vector_3d( 1.0, 1.0, 1.0 )
        is a temporary.and you can't take a non constant reference to a temporary.

        Changing the signature to
        inline vector_3d& operator*(doubl e s, const vector_3d & v)

        should fix the problem.

        --
        Jim Langston
        tazmaster@rocke tmail.com


        Comment

        • Travis

          #5
          Re: beginner's question concerning operator overloading.

          On Apr 28, 9:20 am, Carter <carterch...@gm ail.comwrote:
          I am getting a recurring error with my multiply operator for a vector
          class I am working on and I am uncertain as to the exact reason why
          the operator* fails to match properly in certain circumstances yet
          seems to work properly in others. Any help would be appreciated.
          >
          Thanks in advance,
          >
          Carter.
          >
          (Code follows)
          >
          #include <iostream>
          >
          class vector_3d
          {
          public:
          >
          vector_3d()
          :_x(0.0), _y(0.0), _z(0.0) {}
          >
          vector_3d(doubl e x, double y, double z)
          :_x(x), _y(y), _z(z) {}
          >
          vector_3d(const vector_3d& v)
          :_x( v._x ), _y ( v._y ), _z( v._z ) {}
          >
          void set_x(double x) { _x = x; }
          void set_y(double y) { _y = y; }
          void set_z(double z) { _z = z; }
          >
          double x() const { return _x; }
          double y() const { return _y; }
          double z() const { return _z; }
          >
          private:
          double _x, _y, _z;
          >
          };
          >
          vector_3d& operator*=(vect or_3d & v, double s)
          {
          v.set_x( s * v.x() );
          v.set_y( s * v.y() );
          v.set_z( s * v.z() );
          return v;
          >
          }
          >
          inline vector_3d& operator*(vecto r_3d & v, double s)
          {
          return (v *= s);
          >
          }
          >
          inline vector_3d& operator*(doubl e s, vector_3d & v)
          {
          return (v *= s);
          >
          }
          >
          std::ostream& operator << ( std::ostream& os, const vector_3d& v)
          {
          return os << v.x() << "," << v.y() << "," << v.z() << std::endl;
          >
          }
          >
          int main()
          {
          // vector_3d v(1.0,1.0,1.0);
          vector_3d v = 2.0 * vector_3d(1.0,1 .0,1.0); // this errors out
          std::cout << (2.0 * v); // this does not
          >
          }
          Make your operator's methods of the class and correct the constness.
          So you'd have...

          vector_3d& vector_3d::oper ator*(const vector_3d &rhs)
          {
          this->_x *= rhs._x;
          this->_y *= rhs._y;
          this->_z *= rhs._z;

          return this;
          }

          and so on...

          Comment

          • Carter

            #6
            Re: beginner's question concerning operator overloading.

            Thanks for the help. That solved the problem.

            Comment

            Working...