Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Member initializer does not name a non-static data member

Tags:

c++

I am new to C++ and trying to get an open source C++ project to compile in x-code. The last two lines of this code:

template<typename T>
struct TVector3 : public TVector2<T> {
    T z;
TVector3(T _x = 0.0, T _y = 0.0, T _z = 0.0)
    : TVector2(_x, _y), z(_z)

are throwing the error: Member initializer does not name a non-static data member

Based on (member initializer does not name a non-static data member or base class), I tried changing the code to this:

template<typename T>
struct TVector3 : public TVector2<T> {
    T z;
TVector3(T _x = 0.0, T _y = 0.0, T _z = 0.0)
    : TVector2(_x, _y) 
{ z(_z);}

But I am getting the same error. Here is the code for the super-class, Vector2. How can I resolve this error?

struct TVector2 {
    T x, y;
    TVector2(T _x = 0.0, T _y = 0.0)
        : x(_x), y(_y)
    {}
    double Length() const {
        return sqrt(static_cast<double>(x*x + y*y));
    }
    double Norm();
    TVector2<T>& operator*=(T f) {
        x *= f;
        y *= f;
        return *this;
    }
    TVector2<T>& operator+=(const TVector2<T>& v) {
        x += v.x;
        y += v.y;
        return *this;
    }
    TVector2<T>& operator-=(const TVector2<T>& v) {
        x -= v.x;
        y -= v.y;
        return *this;
    }
};
like image 748
bernie2436 Avatar asked Jan 13 '23 05:01

bernie2436


1 Answers

Inside a class template, only its own name is injected for use without template arguments. You need this:

template<typename T>
struct TVector3 : public TVector2<T> {
    T z;
TVector3(T _x = 0.0, T _y = 0.0, T _z = 0.0)
    : TVector2<T>(_x, _y), z(_z)
like image 118
Angew is no longer proud of SO Avatar answered Feb 08 '23 12:02

Angew is no longer proud of SO