开发者

Visual C++ doesn't see operator<< overload

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 开发者_Go百科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.


I encountered the same problem and I think I figured out what was going on. For some reason, MSVC sometimes mistakes std::endl, for the endl defined in QTextStream (of course if you are "using namespace std" anywhere, this behavior is appropriate).

Also, I think MSVC sometimes gets confused with std::strings (maybe if they are const or addresses or something like that) with QTextStream.

MSVC tends to be very particular about the const/& variants of overloads, especially in cases where there may be some ambiguity. I have seen this before with non-QT code with some overloaded functions.

Of course, the error messages are just confusing so, it might be that my analysis is wrong here.


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 &.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜