我无法为结构中的常量赋值,请遵循代码:
#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;
}
此代码出现以下错误:
错误:标量对象“data”需要初始化器 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;
}
关于c++ - 如何将常量中的值设置为struct c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55383881/