-1

I'm trying to write a helper .h file with some operators.

Mainly something like this

QVector3D &operator=(const someDataObj&data){
      QVector3D out(data[0],data[10],data[12]);
      return out;
}

But when ever I try it I get : error C2801: 'operator =' must be a non-static member

if I try :

Vector3D operator=(QVector3D &left, const someDataObj &other) {}

I get : error C2801: 'operator =' must be a non-static member

I'm just lost... how can I properly do it ?

Edit: I forgot to mention, its main usage will be converting of data from libraryA to libraryB.

halfer
  • 19,824
  • 17
  • 99
  • 186
Dariusz
  • 960
  • 13
  • 36

1 Answers1

3

As the error message explains, assignment operator can only be defined as a member. Assuming QVector3D comes from Qt and is therefore not modifiable by you, and, on the other hand, someDataObj is of a type you've defined, you can make a conversion operator for that type, instead. If that's not the case, then you have to perform the conversion explicitly via a non-member function.

Edit:

The syntax library_a_data = library_b_data, but one of those types must be wrapped in your own:

  1. private inheritance

    class MyVector3D : private QVector3D // do not allow implicit conversions
    // to the base class for safety, as the destructor is not virtual
    {
    public:
        using QVector3D::member_1;
        using QVector3D::member_n; // very tedious work of bringing the names back
    
        // hurray, you can define your assignment or conversion operator
    };
    
  2. composition

    class MyVector3D
    {
        QVector3D m_data;
    
    public:
        auto member_1() { return m_data.member_1(); }
        auto member_n() { return m_data.member_n(); } // even more tedious
    
        // hurray, you can define your assignment or conversion operator
    };
    

However, I think that this kind of conversion should be left explicit, but the above code should be already discouraging enough. Can we consider this as an improvement, since at one point you have to convert to your type? I don't think so.

LogicStuff
  • 19,397
  • 6
  • 54
  • 74
  • Hey, I don't have control over either of the data classes. I'm basically converting data from libraryA to libraryB. So if I have to do "conversion explicitly via a non-member function" Does it mean I have to write ```static QVector3D toQtVec(const someDataObj &other){return QVector3D(other[1],other[4],other[5]);}``` ? – Dariusz May 27 '19 at 09:51
  • Not sure actually, I guess I need it as inline if its header only. Else I'll get duplicate declarations error I think – Dariusz May 27 '19 at 09:55
  • Programming by guessing doesn't work. [Study and learn](https://stackoverflow.com/q/388242/560648) instead. – Lightness Races in Orbit May 27 '19 at 09:56