I have the following template:
Tup4.h:
#pragma once
#include <array>
#include "Comparisons.h"
class Tup4Data {
protected:
std::array<double, 4> v;
};
template <class C>
class Tup4Impl : Tup4Data {
public:
Tup4Impl<C>(std::array<double, 4> vc) :
v(vc) {};
bool equals(const Tup4Impl<C>& t) {
return fuzzyEquality<4>(v, t.v);
}
};
Then I have a small program like this:
#include <iostream>
#include "Comparisons.h"
#include <array>
#include "Tup4.h"
class Vec4 : Tup4Impl<Vec4>{};
int main() {
std::array<double, 4> a1 = {1.0, 1.0, 1.0, 1.0};
std::array<double, 4> a2 = { 1.0, 1.0, 1.0, 1.0 };
Tup4Impl<Vec4> v1 = Tup4Impl<Vec4>(a1);
Tup4Impl<Vec4> v2 = Tup4Impl<Vec4>(a2);
v1.equals(v2);
}
Running this throws the following error: Error C2614 'Tup4Impl': illegal member initialization: 'v' is not a base or member
Why is this initialization illegal? I can fix this code just by changing the Constructor method to
Tup4Impl<C>(std::array<double, 4> vc){
v = vc;
}
But I'm curious as to why the initilization is illegal like this
For clarification: the error occurs in Tup4Impl <Vec4>, meaning that the problem is not in the template but in the class that implements it. v is defined as a protected member in the parent class, so it should be a member, right?