doubts on operator

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

    doubts on operator

    I have a class that defines a generic point where p={a1..an}

    template<class T,int iclass Point
    {
    public:
    T v[i];

    Point<T,i>()
    {
    for (int j=0;j<i;j++) v[j]=0;
    }


    inline T & operator [] ( const int j ){
    assert(j>=0 && j<i);
    return v[j];
    }
    inline const T & operator [] ( const int j ) const {
    assert(j>=0 && j<i);
    return v[j];
    }


    inline Point<T,i>& operator=(const Point<T,i& num)
    {
    for (int j=0;j<i;j++) v[j]=num.v[j];
    return *this;

    }


    inline Point<T,i>& operator=(const float* pnum)
    {
    for (int j=0;j<i;j++) v[j]=pnum[j];
    return *this;

    }


    inline Point<T,ioperat or + ( Point<T,iconst & num) const {
    Point<T,ivect;
    for (int j=0;j<i;j++) vect.v[j]=v[j]+num.v[j];
    return vect;
    }
    inline Point<T,ioperat or - ( Point<T,iconst & num) const {
    Point<T,ivect;
    for (int j=0;j<i;j++) vect.v[j]=v[j]-num.v[j];
    return vect;
    }
    inline Point<T,ioperat or * ( const T s ) const {
    Point<T,ivect;
    for (int j=0;j<i;j++) vect.v[j]=v[j]*s;
    return vect;
    }

    inline Point<T,i>& operator *= ( const T s ){
    for (int j=0;j<i;j++) v[j]=v[j]*s;
    return *this;
    }
    }


    I define a new class Point2

    template<class Tclass Point2: public Point<T,2>
    {
    public:
    Point2<T>()
    {
    v[0]=0;
    v[1]=1;
    }

    Point2<T>(const T& a, const T& b)
    {
    v[0]=a;
    v[1]=b;
    }

    T x() { return v[0] };
    T y() { return v[1] };

    };

    and in main program I run:

    Point2f a;
    Point2f b;
    Point2f c;
    c=a+b;

    I get this error:

    error C2679: binary '=' : no operator found which takes a right-hand
    operand of type 'Point<T,i>' (or there is no acceptable conversion)
    with
    [
    T=float,
    i=2
    ]
    could be 'Point2<T&Point 2<T>::operator =(const Point2<T>
    &)'
    with
    [
    T=float
    ]
    while trying to match the argument list '(Point2f,
    Point<T,i>)'
    with
    [
    T=float,
    i=2
    ]


    Why Point2 don't call the operator= defined in Point<i,T>?

    Point2 is a Point<i,Tbecaus e derive by Point<i,T>, I don't
    understand...


    Please explain me.

  • Victor Bazarov

    #2
    Re: doubts on operator

    alessio211734 wrote:
    I have a class that defines a generic point where p={a1..an}
    >
    template<class T,int iclass Point
    {
    public:
    T v[i];
    Public? Really?
    >
    Point<T,i>()
    First off, you don't need to repeat "<T,i>" *inside* the template class
    definition.
    {
    for (int j=0;j<i;j++) v[j]=0;
    }
    >
    >
    inline T & operator [] ( const int j ){
    assert(j>=0 && j<i);
    return v[j];
    }
    inline const T & operator [] ( const int j ) const {
    assert(j>=0 && j<i);
    return v[j];
    }
    >
    >
    inline Point<T,i>& operator=(const Point<T,i& num)
    {
    for (int j=0;j<i;j++) v[j]=num.v[j];
    return *this;
    >
    }
    >
    >
    inline Point<T,i>& operator=(const float* pnum)
    {
    for (int j=0;j<i;j++) v[j]=pnum[j];
    return *this;
    >
    }
    >
    >
    inline Point<T,ioperat or + ( Point<T,iconst & num) const {
    Point<T,ivect;
    for (int j=0;j<i;j++) vect.v[j]=v[j]+num.v[j];
    return vect;
    }
    inline Point<T,ioperat or - ( Point<T,iconst & num) const {
    Point<T,ivect;
    for (int j=0;j<i;j++) vect.v[j]=v[j]-num.v[j];
    return vect;
    }
    inline Point<T,ioperat or * ( const T s ) const {
    Point<T,ivect;
    for (int j=0;j<i;j++) vect.v[j]=v[j]*s;
    return vect;
    }
    >
    inline Point<T,i>& operator *= ( const T s ){
    for (int j=0;j<i;j++) v[j]=v[j]*s;
    return *this;
    }
    }
    >
    >
    I define a new class Point2
    >
    template<class Tclass Point2: public Point<T,2>
    {
    public:
    Point2<T>()
    {
    v[0]=0;
    v[1]=1;
    }
    >
    Point2<T>(const T& a, const T& b)
    {
    v[0]=a;
    v[1]=b;
    }
    >
    T x() { return v[0] };
    T y() { return v[1] };
    >
    };
    >
    and in main program I run:
    >
    Point2f a;
    So, my guess would be that 'Point2f' is a typedef, something like

    typedef Point2<floatPoi nt2f;

    Next time, please, be verbose about those things.
    Point2f b;
    Point2f c;
    c=a+b;
    >
    I get this error:
    >
    error C2679: binary '=' : no operator found which takes a right-hand
    operand of type 'Point<T,i>' (or there is no acceptable conversion)
    [..]
    Why Point2 don't call the operator= defined in Point<i,T>?
    Because Point2 has its *own* operator=, which *hides* any that it can
    inherit from its base class.
    >
    Point2 is a Point<i,Tbecaus e derive by Point<i,T>, I don't
    understand...
    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask

    Comment

    • alessio211734

      #3
      Re: doubts on operator

      Because Point2 has its *own* operator=, which *hides* any that it can
      inherit from its base class.
      What's the operator= that Point2 define?
      Point2 define a default operator?
      What's the advantages of hereditance if I should re-define an operator
      for Point2 again?
      Can't use operator= and other operators defined in the base class
      without re-define in the derived class Point2?

      Comment

      • Victor Bazarov

        #4
        Re: doubts on operator

        alessio211734 wrote:
        >Because Point2 has its *own* operator=, which *hides* any that it can
        >inherit from its base class.
        >
        What's the operator= that Point2 define?
        It's called "the default copy-assignment operator", provided by the
        compiler. It has the following signature:

        Point2& Point2::operato r=(Point2 const&);
        Point2 define a default operator?
        Yes.
        What's the advantages of hereditance if I should re-define an operator
        for Point2 again?
        "Hereditanc e"? You don't have to redefine the assignment operator, you
        just need to make it visible. See your book about the " 'using'
        declaration".
        Can't use operator= and other operators defined in the base class
        without re-define in the derived class Point2?
        Can.

        V
        --
        Please remove capital 'A's when replying by e-mail
        I do not respond to top-posted replies, please don't ask

        Comment

        • Markus Moll

          #5
          Re: doubts on operator

          Hi

          alessio211734 wrote:
          >Because Point2 has its *own* operator=, which *hides* any that it can
          >inherit from its base class.
          >
          What's the operator= that Point2 define?
          Point2 define a default operator?
          What's the advantages of hereditance if I should re-define an operator
          for Point2 again?
          Can't use operator= and other operators defined in the base class
          without re-define in the derived class Point2?
          An assignment operator for a base class is rarely useful. Consider:
          A a;
          B b;
          a = b;

          Without additional knowledge (i.e. without a user-defined assignment
          operator), this only makes sense if b contains all the parts of an A,
          which is the case if B is equal to or derived from A. If A is derived from
          B, on the other hand, then how would you fill in the missing data?

          Markus

          Comment

          • alessio211734

            #6
            Re: doubts on operator

            On 1 Ago, 16:36, Markus Moll <markus.m...@es at.kuleuven.ac. bewrote:
            Hi
            >
            alessio211734 wrote:
            Because Point2 has its *own* operator=, which *hides* any that it can
            inherit from its base class.
            >
            What's the operator= that Point2 define?
            Point2 define a default operator?
            What's the advantages of hereditance if I should re-define an operator
            for Point2 again?
            Can't use operator= and other operators defined in the base class
            without re-define in the derived class Point2?
            >
            An assignment operator for a base class is rarely useful. Consider:
            A a;
            B b;
            a = b;
            >
            Without additional knowledge (i.e. without a user-defined assignment
            operator), this only makes sense if b contains all the parts of an A,
            which is the case if B is equal to or derived from A. If A is derived from
            B, on the other hand, then how would you fill in the missing data?
            >
            Markus

            I would like define constructors for n-dimensional point.
            If I include in the base class these constructors my application work,
            but I think that this is not safe because
            I can call the constructor with three parameters when I use
            Point<float,2fo r example.


            Point<T,i>(cons t T& a, const T& b)
            {
            assert(i==2);
            v[0]=a;
            v[1]=b;
            }

            Point<T,i>(cons t T& a, const T& b,const T& c)
            {
            assert(i==3);
            v[0]=a;
            v[1]=b;
            v[2]=c;
            }

            I think to delete constructors in base classes and extends the base
            class with
            for example Point2 derived by Point and in this class define the
            constructor with 2 parameters.

            What's syntax should use in Point2 for utilize Point operators?




            Comment

            • Ian Collins

              #7
              Re: doubts on operator

              alessio211734 wrote:
              I have a class that defines a generic point where p={a1..an}
              >
              template<class T,int iclass Point
              {
              public:
              T v[i];
              >
              Point<T,i>()
              {
              for (int j=0;j<i;j++) v[j]=0;
              }
              >
              >
              inline T & operator [] ( const int j ){
              In addition to Victor's comments, the 'inline' is superfluous here. All
              member functions defined in the class declaration are inline.

              --
              Ian Collins.

              Comment

              Working...