I made 2 classes, Point and Point3D:
Point.h:
#ifndef POINT_H
#define POINT_H
#include <iostream>
template <typename T>
class Point
{
protected:
T X,Y,Z;
public:
Point(const T x, const T y, const T z) : X(x), Y(y), Z(z) {}
T getX() {return X;};
T getY() {return Y;};
T getZ() {return Z;};
void Output();
};
template< typename T>
void Point<T>::Output()
{
std::cout << "x: " << X << " y: " << Y << " z: " << Z << std::endl;
}
#endif
Point3D.h:
#ifndef POINT3D_H
#define POINT3D_H
#include "Point.h"
template <typename T>
class Point3D : public Point<T>
{
T X,Y,Z;
public:
Point3D(const T x, const T y, const T z) : Point<T>(x,y,z) {}
void Output3D();
};
template< typename T>
void Point3D<T>::Output3D()
{
std::cout << "3D" << std::endl;
//these both show junk values
//std::cout << "X: " << this->X << std::endl;
//std::cout << "X: " << X << std::endl;
//this says "there are no arguments to 'getX' that depend on a template parameter, so a declaration of 'getX' must be available
double x = getX();
std::cout << "X: " << x << std::endl;
}
#endif
Test.cpp
#include <iostream>
#include "Point3D.h"
int main(int argc, char* argv[])
{
Point3D<double> C(1.2,2.3,4.5);
C.Output(); //works fine
C.Output3D(); //doesn't work
return 0;
}
The problem is with the Output3D function. It doesn't seem to be able to access the protected variables in Point (they just are garbage values). If I try to use the accessors, there are compiler errors (described with comments in the above code).
Can anyone see what I am doing wrong here?
Thanks,
David