我尝试将 QtGStreamer 与 MS Visual Studio 2015 的 C++ 编译器一起使用,该编译器默认为 C++11 标准(或多或少)。
header refpointer.h 包含以下内容:
template <class T, class X>
struct RefPointerEqualityCheck<T, X*>
{
static inline bool check(const RefPointer<T> & self, X* const & other)
{
return self.m_class ? self.m_class->m_object == other : !other;
}
};
...稍后在 refpointer.h 中我们有以下内容:
template <class T>
template <class X>
bool RefPointer<T>::operator==(const X & other) const
{
return Private::RefPointerEqualityCheck<T, X>::check(*this, other);
}
template <class T>
template <class X>
bool RefPointer<T>::operator!=(const X & other) const
{
return !Private::RefPointerEqualityCheck<T, X>::check(*this, other);
}
其他人遇到了我收到的错误消息 here :
qt5gstreamer\qglib\refpointer.h(280): error C2039: 'check': is not a member of 'QGlib::Private::RefPointerEqualityCheck<T,X>'
with
[
T=QGst::BufferList,
X=int
]
QtGStreamer 的主要贡献者之一回复如下:
Hm, looks like the code needs some additions to support C++11's nullptr. Try removing any compiler flags that enable C++11 functionality (including QTGSTREAMER_FLAGS if you are using cmake) and see what happens. Also, it would be nice if you open a bug report about this.
有两件事:
(1) 如果可以修复 header (并且可能提交补丁),我希望避免放弃 C++11 标准支持;和
(2) 我什至不确定 MSVC++15 是否有一个选项可以返回到早期的标准版本(我在谷歌搜索时无法轻松找到它)。
与编译器消息所说的相反,在未经训练的人看来,check
确实定义为 QGlib::Private::的成员RefPointerEqualityCheck
如您所见 here在 QtGStreamer 源代码中(该版本的 header 与我在系统上使用的版本相同。)
问:鉴于上述情况,我的代码是否有问题?可以为 QtGstreamer 贡献一些东西来解决问题;或者我是否最好针对早期版本的 C++ 标准进行编译?
最佳答案
事实证明,我的编译器没有指出我遇到的问题的实际根源。 RefPointer 相等性检查的模板参数是一个巨大的提示。
我遇到这个问题是因为我将 RefPointer
与常量 NULL
进行比较,后者是 #define
d 与 0
(编译为 int
)。
因此,当执行以下操作时:
BufferListPtr buf; //An instance of RefPointer
//I thought that BufferListPtr was equivalent to `QGst::Buffer*` but apparently it has some overloaded operators, which is the source of our problem!
if(buf == NULL) //Compile-time error!
编译器尝试调用RefPointer
的operator==
函数,但发现它不适用于X
参数int
(它只允许您传入指针 (X*
) 或其他RefPointer
。)
因此,您必须仔细检查 QtGstreamer 代码,并找到将 RefPointer
实例与 NULL
与 ==
进行比较的所有时间,或者!=
,并按如下方式修复它们:
if(ptr.isNull())
要记住的是,所有 RefPointer 实例都是值,而不是指针。如果您来自 Visual C++ 世界(就像我一样),并且您希望 LPCSTR
是一个简单的宏,将 *
添加到 CSTR
,不要将其与 QtGstreamer 中的 Ptr
类和 RefPointer
的功能混淆!声明RefPointer
(例如,ElementPtr
)实际上会立即在堆栈上分配该类的实例,因此将其与 NULL 进行比较是没有意义的。
但是你可以对其调用isNull()
来查看它是否为NULL,因为如果你在堆栈上分配它并且你没有做一些奇怪的事情,那么该类将始终被初始化声明一个 ElementPtr*
。
总之,RefPointer
及其所有子类,包括以 Ptr
结尾的 QtGstreamer 对象类型,只是一个封装指向原始 C 类型的指针,因此您必须将它们视为值而不是指针。
关于c++ - 由于 C++11 标准,QtGStreamer header 中出现编译时错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41123505/