c++ passing a const object reference to a function(c ++将const对象引用传递给函数)
问题描述
错误:将const QByteArray"作为QByteArray&"的this"参数传递;QByteArray::append(const QByteArray&)' 丢弃限定符 [-fpermissive]
error: passing 'const QByteArray' as 'this' argument of 'QByteArray& QByteArray::append(const QByteArray&)' discards qualifiers [-fpermissive]
因为在作为函数参数传递时使对象成为 const 是一种约定,所以我已经这样做了.但现在我收到一个错误!!,我不想使函数保持不变,因为我必须将 qbyte 数组中的数据转换为 short,然后将其附加到另一个数组.
since it is a convention to make objects const while passing as function arguments i have done it. but now i am getting an error!!, i dnt want to make the function constant as i have to convert data in qbyte array into short and then append it another array.
QByteArray ba((const char*)m_output.data(), sizeof(ushort));
playbackBuffer.append(ba);
我真的需要把这个数组传入playbackbuffer;
它给我一个关于 playbackBuffer.append(ba);
I really need to pass this array into playbackbuffer;
It is giving me an error on playbackBuffer.append(ba);
请帮忙
提前致谢
please help
thanks in advance
推荐答案
这意味着您正在对 const 成员调用非常量成员函数.据推测,您的 append 函数修改了字节数组.使用 const 引用,您不应该修改.
This means you are calling a non-const member function on a const member. Presumably, your append function modifies the byte array. With a const reference, you shouldn't be modifying.
这篇关于c ++将const对象引用传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:c ++将const对象引用传递给函数
基础教程推荐
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- CString 到 char* 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
