使用 Qt:使用 QList时从 const void* 到 void* 的转换无效;

发布于 2024-12-02 10:42:03 字数 1686 浏览 1 评论 0原文

我已经在 Qt 和 C++ 上摆弄了一段时间了,但是我遇到了这个错误,并且似乎无法弄清楚它为什么会出现。还有很多其他问题已经通过 const void* 转换错误消息得到了解答,但我无法真正看到这些解释对我的情况有什么帮助,所以这里是:

我有一个 QList< 的重新实现“MyTypeManager” ; MyType *const>,因此指向非 const MyType 的 const 指针列表。但是,当我重新实现中的函数调用 addMyType 时,

void MyTypeManager::addMyType(MyType *const var)
{
     this->append(var);
}

会发生以下错误:

In file included from /usr/include/qt4/QtCore/QList:1:0,
             from ../qtsdlthread/mytypemanager.h:4,
             from ../qtsdlthread/mytypemanager.cpp:1:
/usr/include/qt4/QtCore/qlist.h: In member function ‘void QList<T>::node_construct(QList<T>::Node*, const T&) [with T = MyType* const]’:
/usr/include/qt4/QtCore/qlist.h:499:13:   instantiated from ‘void QList<T>::append(const T&) [with T = MyType* const]’
../qtsdlthread/mytypemanager.cpp:20:26:   instantiated from here
/usr/include/qt4/QtCore/qlist.h:359:58: error: invalid conversion from ‘const void*’ to ‘void*’
/usr/include/qt4/QtCore/qlist.h: In member function ‘void QList<T>::node_copy(QList<T>::Node*, QList<T>::Node*, QList<T>::Node*) [with T = MyType* const]’:
/usr/include/qt4/QtCore/qlist.h:666:9:   instantiated from ‘QList<T>::Node* QList<T>::detach_helper_grow(int, int) [with T = MyType* const]’
/usr/include/qt4/QtCore/qlist.h:497:48:   instantiated from ‘void QList<T>::append(const T&) [with T = MyType* const]’
../qtsdlthread/mytypemanager.cpp:20:26:   instantiated from here
/usr/include/qt4/QtCore/qlist.h:386:17: error: invalid conversion from ‘const void*’ to ‘void*’

mytypemanager 中的 20:26 是上面发布的 this->append 行。

I've been messing around with Qt and C++ for a while, but I've run into this error and can't seem to figure out why it crops up. There are a lot of other questions that have been answered out there with the const void* conversion error message, but I can't really see how the explanations help in my case, so here goes:

I have a reimplementation 'MyTypeManager' of QList< MyType *const>, so a list of const pointers to non-const MyTypes. However, when a function in my reimplementation, addMyType is called

void MyTypeManager::addMyType(MyType *const var)
{
     this->append(var);
}

the following error(s) occur:

In file included from /usr/include/qt4/QtCore/QList:1:0,
             from ../qtsdlthread/mytypemanager.h:4,
             from ../qtsdlthread/mytypemanager.cpp:1:
/usr/include/qt4/QtCore/qlist.h: In member function ‘void QList<T>::node_construct(QList<T>::Node*, const T&) [with T = MyType* const]’:
/usr/include/qt4/QtCore/qlist.h:499:13:   instantiated from ‘void QList<T>::append(const T&) [with T = MyType* const]’
../qtsdlthread/mytypemanager.cpp:20:26:   instantiated from here
/usr/include/qt4/QtCore/qlist.h:359:58: error: invalid conversion from ‘const void*’ to ‘void*’
/usr/include/qt4/QtCore/qlist.h: In member function ‘void QList<T>::node_copy(QList<T>::Node*, QList<T>::Node*, QList<T>::Node*) [with T = MyType* const]’:
/usr/include/qt4/QtCore/qlist.h:666:9:   instantiated from ‘QList<T>::Node* QList<T>::detach_helper_grow(int, int) [with T = MyType* const]’
/usr/include/qt4/QtCore/qlist.h:497:48:   instantiated from ‘void QList<T>::append(const T&) [with T = MyType* const]’
../qtsdlthread/mytypemanager.cpp:20:26:   instantiated from here
/usr/include/qt4/QtCore/qlist.h:386:17: error: invalid conversion from ‘const void*’ to ‘void*’

20:26 in mytypemanager is the this->append line posted above.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尘曦 2024-12-09 10:42:03

来自文档

QList 的值类型必须是可分配的数据类型。

可惜 MyType *const 不可分配。您有几种补救措施:

1. 使 T 成为可变指针

2. 使 T 成为指向 const 指针的指针:

typedef MyType *const Element

void MyTypeManager::addMyType(Element var)
{
    Element* ptr2ptr = new Element(var);
    this->append(ptr2ptr);
}

但是现在您需要担心 2 级内存管理。

3.(危险)将 T=MyType* 和 const-cast MyType *const 转换成 MyType *

this->append(const_cast<MyType *>(var));

这只有在你确定的情况下才有效var 最初是作为 MyType* 变量创建的。

From the documentation:

QList's value type must be an assignable data type.

Alas MyType *const is not assignable. You have several remedies:

1. Make T a mutable pointer

2. Make T a pointer to your const pointer:

typedef MyType *const Element

void MyTypeManager::addMyType(Element var)
{
    Element* ptr2ptr = new Element(var);
    this->append(ptr2ptr);
}

But now you have 2 levels of memory management to worry about.

3. (Dangerous) Make T=MyType* and const-cast MyType *const into MyType *:

this->append(const_cast<MyType *>(var));

This will only work if you are certain var was originally created as a MyType* variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文