c++ - 如何将常量中的值设置为 struct c++
问题描述
我无法为结构中的常量赋值,请遵循以下代码:
#include <iostream>
#include <stdio.h>
typedef struct
{
float a;
float b;
float c;
float intensity;
} PointXYZI;
typedef struct structParent{
int x;
int y;
const PointXYZI* xyzi;
} structParent;
int main()
{
float o = 10.f, p = 5.0f, z = 96.0f;
PointXYZI points = {o, p, z};
const structParent *data = {0,0, &points};
std::cout << " *-* " << data.xyzi->c << std::endl;
std::cout << " *-* " << points.a << std::endl;
return 0;
}
我收到以下代码错误:
错误:标量对象“数据”在初始化程序中需要一个元素 const structParent *data = {0,0, &points};
谢谢...
解决方案
@UnholySheep 答案的示例版本解释如下。
void someFunc(const structParent &x)
// ^^^^^^
{
std::cout << " @_@ " << x.xyzi->c << std::endl;
}
int main()
{
float o = 10.f, p = 5.0f, z = 96.0f;
PointXYZI points = {o, p, z, 0};
// ^^^^^
const structParent data = {0,0, &points};
// ^^^
std::cout << " *-* " << data.xyzi->c << std::endl;
std::cout << " *-* " << points.a << std::endl;
someFunc(data);
// ^^^^^^^
return 0;
}
推荐阅读
- python - 在全息视图中访问散景(图)参数
- c++ - 从源代码构建时,boost iostreams 中的编译错误
- linux - 使用 airmon-ng 收听特定频道
- android - cursor.moveToNext() 抛出 CursorWindowAllocationException
- reactjs - React.useState 不会从 props 重新加载状态
- docker - 如何从私有存储库重新启动 Cloud Foundry Docker 应用程序?
- wpf - 使用 mage.exe 自动执行扩展验证 (EV) 代码签名
- jquery - MCE5-如何在 Tiny mce-5 中将任何图像作为自定义按钮图标?
- kubernetes - Kubernetes 本地存储类 (kubernetes.io/no-provisioner) 保留策略卷
- php - 数据表中的无效 JSON 响应