4

The function below converts the contents which is stored in the class cVector, to a std::vector and returns this.

template <class T> std::vector<T> cVector<T>::convertToStdVector()
{
    std::vector<T> vec;
    vec.resize(m_nSize);
    for (unsigned i = 0; i < m_nSize; ++i) {
        vec[i] = m_pData[i];
    }
    return vec;
}

The above works perfect, now instead of using a function like this, I would like to overload the assignment operator and basically do the same thing.

I've tried the following:

template <class T> class cVector
{
public:

    cVector(unsigned nSize, AllocEnum eAllocType=MALLOC);
    cVector(T *pData, unsigned nSize, bool bCopy=false);

    // convert to std vector
    std::vector<T> operator=(const cVector<T> &cvec);

    //..
    //....
}

template <class T> std::vector<T> cVector<T>::operator=(const cVector<T> &cvec)
{
    std::vector<T> vec;
    vec.resize(m_nSize);
    for (unsigned i = 0; i < m_nSize; ++i) {
        vec[i] = m_pData[i];
    }
    return vec;
}

This compiles fine, however when I try to call it in my code, e.g.

std::vector<float> vec = cVectorInstance;

Than I get the following error during compilation:

error: conversion from 'cVector' to non-scalar type 'std::vector >' requested"

I'm not sure what is going wrong here, I hope somebody can help / explain.

5
  • You're not supposed to overload with return value. This is pure wrong. Commented Jul 26, 2017 at 9:40
  • assignement operators should return a reference to *this Commented Jul 26, 2017 at 9:42
  • Why would you want to write your own vector class in the first place? Whatever you are trying to achieve is most likely already possible (and implemented in a better way) with std::vector. Commented Jul 26, 2017 at 9:42
  • That is non constructive Florian. Even though it might seem not useful to you, that doesn't mean that projects / codebases exist which contain a vector class, it might be due legacy which is not easy to quickly remove, or it might be that project specific requirements exist which demanded such a class. Either way it lies completely out of the scope of this question to tell you the project specific details I'm working on. Commented Jul 26, 2017 at 9:52
  • If your cVector class has public methods to return a pointer to the data buffer, then you could have simply used the various constructors available for std::vector instead of writing template classes, or if assigning, use std::copy using std::back_inserter to insert items into the new vector. Commented Jul 26, 2017 at 10:08

2 Answers 2

5

The assignment operator you defined is actually for asignment of a cVector to another cVector, not a conversion to std::vector, which is what you want.

What you are looking for is overloading the conversion operator:

operator std::vector< T >() const { ... }
Sign up to request clarification or add additional context in comments.

Comments

1

The assignment operator usually looks somewhat like this:

class C 
{
public:
    C& operator=(const C& other) {...}
};

It is used to assign something to an instance of the class C not to something of another class. You can overload it, so that it takes objects of other classes and assigns them to your class but not the other way around. So

cVector& operator=(const std::vector<...>& other) { ... }

would take the values inside other and assign them to your cVector. If you want to do the other thing just use your convertToStdVector function.

2 Comments

"The assignment operator usually looks somewhat like this" - No, it does not. Its standard signature is such that it returns C&.
@ChristianHackl Yes you're right... I got confused by the comments... Fixed it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.