![](/img/trans.png)
[英]Error 'initializer does not name a non-static data member or base class' when the initializer is the base class name
[英]Member initializer does not name a non-static data member
我是C ++的新手,並試圖讓一個開源的C ++項目在x-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)
拋出錯誤:成員初始值設定項未命名非靜態數據成員
基於( 成員初始化程序沒有命名非靜態數據成員或基類 ),我嘗試將代碼更改為:
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);}
但我得到了同樣的錯誤。 這是超類Vector2的代碼。 我該如何解決這個錯誤?
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;
}
};
在類模板中,只注入自己的名稱而不使用模板參數。 你需要這個:
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.