Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6993189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:43:59+00:00 2026-05-27T19:43:59+00:00

I have some trouble with class template inheritance and operators (operator +), please have

  • 0

I have some trouble with class template inheritance and operators (operator +),
please have a look at these lines:

Base vector class (TVector.h):

template<class Real, int Size>
class TVector {
protected:
    Real* values;
public:

    ...

    virtual TVector<Real, Size>& operator=(const TVector<Real, Size>& rTVector) { //WORKS
        int i = 0;
        while (i<Size) {
            *(values+i) = *(rTVector.values+i);
            i++;
        }
        return *this;
    }
    virtual TVector<Real, Size> operator+(const TVector<Real, Size>& rTVector) const {
        int i = 0;
        Real* mem = (Real*)malloc(sizeof(Real)*Size);
        memcpy(mem, values, Size);
        while (i<Size) {
            *(mem+i) += *(rTVector.values+i);
            i++;
        }
        TVector<Real, Size> result = TVector<Real, Size>(mem);
        free(mem);
        return result;
    }
};

2D vector class (TVector2.h):

template<class Real>
class TVector2: public TVector<Real, 2> {
public:

    ...

    TVector2& operator=(const TVector2<Real>& rTVector) { //WORKS
        return (TVector2&)(TVector<Real, 2>::operator=(rTVector));
    }
    TVector2 operator+(TVector2<Real>& rTVector) const { //ERROR
        return (TVector2<Real>)(TVector<Real, 2>::operator+(rTVector));
    }
};

Test (main.cpp):

int main(int argc, char** argv) {
    TVector2<int> v = TVector2<int>();
    v[0]=0;
    v[1]=1;
    TVector2<int> v1 = TVector2<int>();
    v1.X() = 10;
    v1.Y() = 15;
    v = v + v1; //ERROR ON + OPERATOR
    return 0;
}

Compilation error (VS2010):

Error 2 error C2440: ‘cast de type’ : cannot convert from
‘TVector<Real,Size>’ to ‘TVector2’ …

What is wrong here ? is there a way to do this kind of stuff ?
Just looking for a way to not redefine all my Vectors classes.
I keep searching to do it, but I will be glad to get some help from you guys.

Sorry for bad English,
Best regards.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:43:59+00:00Added an answer on May 27, 2026 at 7:43 pm
    #include <memory>
    
    using namespace std;
    
    template<class Real, int Size> class TVector {
    protected:
      Real *_values;
    public:
      TVector() {
        // allocate buffer
        _values = new Real[Size];
      }
      TVector(Real *prValues) {
        // check first
        if (prValues == 0)
          throw std::exception("prValues is null");
        // allocate buffer
        _values = new Real[Size];
        // initialize buffer with values
        for (unsigned int i(0U) ; i < Size ; ++i)
          _values[i] = prValues[i];
      }
      // Do not forget copy ctor
      TVector(TVector<Real, Size> const &rTVector) {
        // allocate buffer
        _values = new Real[Size];
        // initialize with other vector
        *this = rTVector;
      }
      virtual ~TVector() {
        delete [] _values;
      }
      virtual Real &operator[](int iIndex) {
        // check for requested index
        if (iIndex < 0 || iIndex >= Size)
          throw std::exception("requested index is out of bounds");
        // index is correct. Return value
        return *(_values+iIndex);
      }
      virtual TVector<Real, Size> &operator=(TVector<Real, Size> const &rTVector) {
        // just copying values
        for (unsigned int i(0U) ; i < Size ; ++i)
          _values[i] = rTVector._values[i];
        return *this;
      }
      virtual TVector<Real, Size> &operator+=(TVector<Real, Size> const &rTVector) {
        for (unsigned int i(0U) ; i < Size ; ++i)
          _values[i] += rTVector._values[i];
        return *this;
      }
      virtual TVector<Real, Size> operator+(TVector<Real, Size> const &rTVector) {
        TVector<Real, Size> tempVector(this->_values);
        tempVector += rTVector;
        return tempVector;
      }
    };
    
    template<class Real> class TVector2: public TVector<Real, 2> {
    public:
      TVector2() {};
      TVector2(Real *prValues): TVector(prValues) {}
      TVector2 &operator=(TVector2<Real> const &rTVector) {
        return static_cast<TVector2 &>(TVector<Real, 2>::operator=(rTVector));
      }
      TVector2 &operator+=(TVector2<Real> const &rTVector) {
        return static_cast<TVector2 &>(TVector<Real, 2>::operator+=(rTVector));
      }
      TVector2 operator+(TVector2<Real> const &rTVector) {
        return static_cast<TVector2 &>(TVector<Real, 2>::operator+(rTVector));
      }
      Real &X() { return _values[0]; }
      Real &Y() { return _values[1]; }
    };
    
    int main(int argc, char** argv) {
      TVector2<int> v = TVector2<int>();
      v[0]=0;
      v[1]=1;
      TVector2<int> v1 = TVector2<int>();
      v1.X() = 10;
      v1.Y() = 15;
      v = v1;
      v += v1;
      v = v + v1;
      return 0;
    }
    

    Some misc notes:

    1. it’s very bad that you use malloc against of new. Real can be POD only to allow vector work well in your case. Use new or provide custom creation policy if you think that malloc provides better performance on PODs. Also do not forget to use delete [] instead of free while destroying memory buffer.
    2. It’s better to perform bounds checking while overloading operator[]
    3. for better performance use ++i instead of postfix form. In former no temporary value is created.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm have some trouble with the fulltext CONTAINS operator. Here's a quick script to
iI have some trouble getting sth like this to work in java: public class
Having some trouble passing values from one class to another. I basically have a
I have some trouble with my trac installation (version 11.4). What should I do
I have some trouble with jquery ui-events: In my application, there are some sliders.
I have some trouble achieving adequate real-time performance from my application and wondering if
I am mlearning javascript and have some trouble creating an onject via prototype. I
I'm migrating to Nhibernate 2.0 GA but have some trouble with setting cache expirations
Good Morning, i have some Trouble with the Zend Framework and Zend_DB_Select, i want
I am developing a small windows app, but have some trouble deciding whether to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.