I'm getting confused with implicit and explicit declarations. I don't know why you need to explicitly say or at certain times. For example,
In my main.cpp
#include <iostream>
#include "Point.h"
int main()
{
Point<int> i(5, 4);
Point<double> *j = new Point<double> (5.2, 3.3);
std::cout << i << *j;
Point<int> k;
std::cin >> k;
std::cout << k;
}
for Point<int> k
. Why do I have to use the explicit declaration? I get compile errors otherwise. Or do I have it coded incorrectly in my Point.h file?
Point.h:
#ifndef POINT_H
#define POINT_H
#include <iostream>
template <class T>
class Point
{
public:
Point();
Point(T xCoordinate, T yCoordinate);
template <class G>
friend std::ostream &operator<<(std::ostream &out, const Point<G> &aPoint);
template <class G>
friend std::istream &operator>>(std::istream &in, const Point<G> &aPoint);
private:
T xCoordinate;
T yCoordinate;
};
template <class T>
Point<T>::Point() : xCoordinate(0), yCoordinate(0)
{}
template <class T>
Point<T>::Point(T xCoordinate, T yCoordinate) : xCoordinate(xCoordinate), yCoordinate(yCoordinate)
{}
template <class G>
std::ostream &operator<<(std::ostream &out, const Point<G> &aPoint)
{
std::cout << "(" << aPoint.xCoordinate << ", " << aPoint.yCoordinate << ")";
return out;
}
template <class G>
std::istream &operator>>(std::istream &in, const Point<G> &aPoint)
{
int x, y;
std::cout << "Enter x coordinate: ";
in >> x;
std::cout << "Enter y coordinate: ";
in >> y;
Point<G>(x, y);
return in;
}
#endif