Substantially cut down code:
Matrix.h
#ifndef MATRIX_H
#define MATRIX_H
#include <algorithm>
#include <stdexcept>
namespace Maths
{
namespace impl
{
static const char AT_DOMAIN_MSG[] = "Index must be non-negative and less than or equal to the relevant dimension template parameter.";
}
template <unsigned height_, unsigned width_> class Matrix
{
protected:
float elems[height_ * width_];
public:
static const unsigned height = height_;
static const unsigned width = width_;
Matrix() { std::fill(elems, elems + height * width, 0.0f); }
Matrix(float (&elems)[height * width]) { std::copy(elems, elems + height * width, this->elems); }
};
typedef Matrix<4, 4> Matrix44;
typedef Matrix<3, 3> Matrix33;
}
#endif
Vector.h
#ifndef VECTOR_H
#define VECTOR_H
#include <cmath>
#include "Matrix.h"
namespace Maths
{
template <unsigned dimension_> struct Vector : Matrix<dimension_, 1>
{
static const unsigned dimension = dimension_;
Vector(const Matrix<dimension, 1>& v)
: Matrix<dimension, 1>(v)
{}
float operator[](int index) const
{
if (index >= dimension || index < 0)
throw std::domain_error(impl::AT_DOMAIN_MSG);
/* !!! ERROR HERE !!! - elems undeclared identifier? */
return elems[index];
}
};
struct Vector3 : Vector<3>
{
float x() const { return operator[](0); }
float y() const { return operator[](1); }
float z() const { return operator[](2); }
Vector3(const Matrix<3, 1>& v)
: Vector(v)
{}
};
}
#endif
Error is that elems in Vector.h is apparently undeclared. What am I doing wrong? The error disappears if I don't disable language extensions (MSVC++ 2008EE), but that's not really a solution. Is there a way to get VS to tell you when it's using an extension?