Sign in to follow this  
password

class template help

Recommended Posts

I have a class that looks like this.
template<class T> class DataType {
  public:
    DataType(T v) : value(v) { }
    T getValue() { return value; }
    ostream & operator<<(ostream &os);
    
    T operator+(DataType<T> &d);
    void operator=(DataType<T> &d);
         
  private:
    T value;
};

Everything is working except the ostream function. I want to be able to use it with cout. The function is defined like this:
template<class T> ostream & DataType<T>::operator<<(ostream &os) {
    os << value;
    return os;
}

It compiles successfully when I leave it like this without actually using cout, but when I use cout together with any instance of this class, error messages like the following appears. no match for 'operator<<' in 'std::cout << length' candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>&(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>] What is wrong? The only thing I can think of is that the ostream function must be a normal function used as a friend in the class, because that is the way I did it last time when it worked. I can't find any way to do that when I use templates though.

Share this post


Link to post
Share on other sites
From the top of my head: forward declaration of the function template, friend declaration of an instance of that template, and implementation of the template.
Your code fails to compile because member operators only allow the owner object to be placed to their left (and, in the case of streams, is placed to the right).


template <typename T>
ostream& operator<<(ostream &, const DataType<T> &);

template<class T>
class DataType {
public:
DataType(T v) : value(v) { }
T getValue() { return value; }
friend ostream & operator<< <T>(ostream &, const DataType<T> &);

T operator+(DataType<T> &d);
void operator=(DataType<T> &d);

private:
T value;
};


template <typename T>
ostream& operator<<(ostream & o, const DataType<T> & d) {
return o << d.value;
}

Share this post


Link to post
Share on other sites
The error messages disappeared, but another 3 appeared instead.

In instantiation of `DataType<float>':

Line 50: instantiated from here

Line 21: template-id `operator<< <float>' for `std::basic_ostream<char, std::char_traits<char> >& operator<<(std::basic_ostream<char, std::char_traits<char> >&, DataType<float>&)' does not match any template declaration

The lines they are pointing to are the following:

DataType<float> grade(15.04); // this line worked before (line 50)
friend ostream & operator<< <T>(ostream &, DataType<T> &); // line 21

Share this post


Link to post
Share on other sites
That's not your fault, I removed the const because I got an error message complaining about it. passing `const DataType<float>' as `this' argument of `T DataType<T>::getValue() [with T = float]' discards qualifiers. The error message disappeared when I removed const for some reason.

Share this post


Link to post
Share on other sites
DataType::getValue() should be marked as const as well (i.e. T getValue() const { return value; }). Then you should be able to reintroduce the original const and not have it produce errors.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this