简体   繁体   中英

Member initializer does not name a non-static data member

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;
    }
};

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM