I am new to C++ and trying to get this open source program (developed for/in linux) to compile and run in xcode on OS X.
When I compiler and run the code I get a LOT of errors (more than xcode is willing to count) like this use of undeclared identifier 'x'
or this use of undeclared identifier 'y'
Here is a sample of the code throwing the error:
template<typename T>
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;
}
};
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)
{}
double Length() const {
return sqrt(static_cast<double>(x*x + y*y + z*z)); //use of undeclared identifier x
}
double Norm();
TVector3<T>& operator*=(T f) {
x *= f;
y *= f;
z *= f;
return *this;
}
To my eye as an inexperienced C++ programmer, it looks like x and y are simply undeclared local variables. I can get the compiler to get rid of the errors by simply declaring the variables, like this...
struct TVector3 : public TVector2<T> {
T z;
T x;
T y;
However, the sheer number of these errors makes me think that
- There might be (reasonably common) versions of C++ compilers that allow you to declare a variable x as _x. That would explain why the source I downloaded has so many compiler errors.
- Maybe I got a "bad batch" of the source and I should not waste my time getting it to compile because the source is screwy somehow.
Can an experienced C++ developer explain what might be going on?