不能将 "const <Component> *" 类型的值分配给类型 "<Component> *" 的实体
A value of type "const <Component> *" cannot be assigned to an entity of type "<Component> *"
我在C++中有一个头文件,其中包含命名空间和类:
namespace imaging
{
class Image
{
protected:
Component *buffer; // To index individual channels
Image(unsigned int width, unsigned int height, const Component *data_ptr, bool interleaved=false); // Holds the image data
}
}
当我尝试实现构造函数时,出现错误:a value of type "const <Component> *" cannot be assigned to an entity of type "<Component> *"
:
#include Image.h
namespace imaging
{
Image::Image(unsigned int width, unsigned int height, const Component *data_ptr, bool interleaved=false)
{
this->height = height;
this->width = width;
buffer = data_ptr; // The error is here!
}
}
data_ptr
是const Component *
,而Image::buffer
是Component *
通过影响第一个到第二个,您将丢弃const
。此属性的全部目的是保护数据,应通过简单的强制转换将其删除。
您可以编辑构造函数参数的类型以删除const
或使用
buffer=const_cast<Component*>(data_ptr);
无论如何,想想你想要的行为。指针是常量(它不是参考)有什么意义吗?
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 防止主数据类型C++的隐式转换
- 大量序列中核苷酸类型的快速计数
- 如何从C++中的依赖类型中获得它所依赖的类型
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如何获取std::result_of函数的返回类型
- 从父命名空间重载类型
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 访问者访问变体并返回不同类型时出错
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 模板元程序查找相似的连续类型名称
- 是否可以从int转换为enum类类型
- 构造函数正在调用一个使用当前类类型的函数
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误c++visual studio c2227左侧'->;Init';必须指向类/结构/联合/泛型类型