Friends, templates, overloading <<

Posted by Crystal on Stack Overflow See other posts from Stack Overflow or by Crystal
Published on 2010-06-03T06:54:23Z Indexed on 2010/06/03 7:14 UTC
Read the original article Hit count: 203

Filed under:
|
|

I'm trying to use friend functions to overload << and templates to get familiar with templates. I do not know what these compile errors are:

Point.cpp:11: error:  shadows template parm 'class T'
Point.cpp:12: error: declaration of 'const Point<T>& T'

for this file

#include "Point.h"

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 T>
std::ostream &operator<<(std::ostream &out, const Point<T> &T)
{
    std::cout << "(" << T.xCoordinate << ", " << T.yCoordinate << ")";
    return out;
}

My header looks like:

#ifndef POINT_H
#define POINT_H

#include <iostream>

template <class T>
class Point
{
public:
    Point();
    Point(T xCoordinate, T yCoordinate);
    friend std::ostream &operator<<(std::ostream &out, const Point<T> &T);

private:
    T xCoordinate;
    T yCoordinate;
};

#endif

My header also gives the warning:

Point.h:12: warning: friend declaration 'std::ostream& operator<<(std::ostream&, const Point<T>&)' declares a non-template function

Which I was also unsure why. Any thoughts? Thanks.

© Stack Overflow or respective owner

Related posts about c++

Related posts about template