0

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

  1. 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.
  2. 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?

bernie2436
  • 22,841
  • 49
  • 151
  • 244

1 Answers1

4
  • x and y are data members of the base class, TVector2<T>.

  • Because the base class is a type that is dependent on a template parameter T, it is not searched when looking up unqualified names.

  • I believe MSVC used to compile this code, not sure whether it still does in C++11 mode. The reason is that MSVC didn't do name resolution properly in templates.

  • The fix is usually to say this->x instead of x.

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
  • I've seen some references to `using x;` working for functions, would that also work for variables? – Adam Oct 03 '13 at 01:03
  • @Adam, `using TVector2::x;` should. – chris Oct 03 '13 at 01:05
  • @Steve: Visual Studio compiler is not doing two phase lookup *yet*, although I saw in a presentation that it is planned for the future, although most of C++14 features had higher priority than implementing two phase lookup. – David Rodríguez - dribeas Oct 03 '13 at 02:05