views:

79

answers:

2

I have a vector class that I want to be able to input/output from a QTextStream object. The forward declaration of my vector class is:

namespace util {
  template <size_t dim, typename T>
  class Vector;
}

I define the operator<< as:

namespace util {
template <size_t dim, typename T>
QTextStream& operator<<(QTextStream& out, const util::Vector<dim,T>& vec)
{
   ...
}

template <size_t dim, typename T>
QTextStream& operator>>(QTextStream& in,util::Vector<dim,T>& vec)
{
   ..
}
}

However, if I ty to use these operators, Visual C++ returns this error:

error C2678: binary '<<' : no operator found which takes a left-hand operand of type 'QTextStream' (or there is no acceptable conversion)

A few things I tried:

  • Originaly, the methods were defined as friends of the template, and it is working fine this way with g++.
  • The methods have been moved outside the namespace util
  • I changed the definition of the templates to fit what I found on various Visual C++ websites.

The original friend declaration is:

friend QTextStream& operator>>(QTextStream& ss, Vector& in) { ... }

The "Visual C++ adapted" version is:

friend QTextStream& operator>> <dim,T>(QTextStream& ss, Vector<dim,T>& in);

with the function pre-declared before the class and implemented after. I checked the file is correctly included using:

#pragma message ("Including vector header")

And everything seems fine. Doesn anyone has any idea what might be wrong?

Note: the definition of the operator doesn't even appears in the list of operator<< found.

A: 

You forgot to make the overload that took a const Vector actually const.

DeadMG
Free functions (i.e. non-member functions), such as these operators, cannot be `const`.
Thomas
A: 

It is hard to say without seeing the actual instantiation site, but so far what I noticed is that the error says that there is no suitable operator for QTextStream, and your implementations use QTextStream&. This might be because you are trying to use the operator on an R-Value, these can be converted to const &, but not only &.

Ramon Zarazua