繁体   English   中英

修改 C++ 结构的特征成员的问题

[英]Problem Modifying Eigen Members of a C++ Structure

我有以下 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的容器对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM