我有一个向量类,我希望能够从 QTextStream 对象输入/输出。我的向量类的前向声明是:
namespace util {
template <size_t dim, typename T>
class Vector;
}
我将运算符 << 定义为:
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)
{
..
}
}
但是,如果我想使用这些运算符,Visual C++ 将返回此错误:
error C2678: binary '<<' : no operator found which takes a left-hand operand of type 'QTextStream' (or there is no acceptable conversion)
我尝试过的一些操作:
- 最初,方法被定义为模板的友元,并且这种方式在 g++ 中工作得很好。
- 这些方法已移至命名空间 util 之外,
- 我更改了模板的定义以适应我在各种 Visual C++ 网站上找到的内容。
原始友元声明是:
friend QTextStream& operator>>(QTextStream& ss, Vector& in) { ... }
“Visual C++ 改编”版本是:
friend QTextStream& operator>> <dim,T>(QTextStream& ss, Vector<dim,T>& in);
函数在类之前预先声明并在类之后实现。我使用以下命令检查了文件是否正确包含:
#pragma message ("Including vector header")
一切似乎都很好。有谁知道可能出了什么问题吗?
注意:运算符的定义甚至不会出现在运算符列表中<<成立。
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.
发布评论
评论(3)
我遇到了同样的问题,我想我知道发生了什么事。由于某种原因,MSVC 有时会将 std::endl 误认为是 QTextStream 中定义的 endl(当然,如果您在任何地方“使用命名空间 std”,这种行为是合适的)。
另外,我认为 MSVC 有时会与 QTextStream 的 std::strings (也许它们是 const 或地址或类似的东西)混淆。
MSVC 往往对 const/& 非常挑剔。重载的变体,特别是在可能存在一些歧义的情况下。我之前在带有一些重载函数的非 QT 代码中见过这种情况。
当然,错误消息只是令人困惑,因此,我的分析可能是错误的。
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.
如果没有看到实际的实例化站点,很难说,但到目前为止,我注意到错误表明 QTextStream 没有合适的运算符,而您的实现使用 QTextStream&。这可能是因为您尝试在 R 值上使用运算符,这些运算符可以转换为 const &,但不仅仅是 &。
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 &.
您忘记将采用 const Vector 的重载实际上设置为 const。
You forgot to make the overload that took a const Vector actually const.