我具有以下C ++结构
struct voxel {
const std::vector<Eigen::ArrayXf>& getVoltage() const { return m_voltage; }
const std::vector<int>& getChannelIndex() const { return m_channelIndex; }
float getx() { return m_x; }
float gety() { return m_y; }
float getz() { return m_z; }
void appendVoltage(Eigen::ArrayXf v) { m_voltage.push_back(v); }
void appendChannelIndex(int c) { m_channelIndex.push_back(c); }
void setPosition(float x, float y, float z) { m_x = x; m_y = y; m_z = z; }
void change(int c) { m_voltage.at(c)(0) = -100; std::cout << m_voltage.at(c) << "\n"; }
private:
std::vector<Eigen::ArrayXf> m_voltage;
std::vector<int> m_channelIndex;
float m_x;
float m_y;
float m_z;
};
下面的类中使用上述结构的数组
class voxelBuffer {
public:
std::vector<voxel> voxels;
voxel getVoxel(ssize_t voxelId) { return voxels.at(voxelId); };
const std::vector<Eigen::ArrayXf>& getVoltage2(ssize_t voxelId) const { return voxels.at(voxelId).getVoltage(); };
ssize_t getNumVoxels() { return voxels.size(); }
};
例如:
voxel vx1;
vx1.setPosition(1.1, 2.1, 3.1);
vx1.appendChannelIndex(1);
vx1.appendVoltage(Eigen::ArrayXf::LinSpaced(10, 0.0, 10 - 1.0));
vx1.appendChannelIndex(2);
vx1.appendVoltage(Eigen::ArrayXf::LinSpaced(20, 0.0, 20 - 1.0));
vx1.appendChannelIndex(3);
vx1.appendVoltage(Eigen::ArrayXf::LinSpaced(30, 0.0, 30 - 1.0));
voxelBuffer vxbuffer;
vxbuffer.voxels.push_back(vx1);
我尝试更改voxel
的第一个数组
vxbuffer.getVoxel(0).change(0);
std::cout << vxbuffer.getVoltage2(0).at(0) << "\n";
但是该元素仍未修改。有人可以帮我解决这个问题吗?
vxbuffer.getVoxel(0)
未返回容器中对voxel
的引用。您正在返回它的副本,因为您的返回类型为voxel getVoxel(ssize_t voxelId)
为voxel
,而不是voxel&
。
然后您在类型为.change(0)
的临时对象上调用voxel
,而不是在容器中的voxel
对象上调用。