【问题标题】:Getter for dynamic array in c++ not workingc++中动态数组的getter不起作用
【发布时间】:2015-08-27 05:50:12
【问题描述】:

我在我的一个类中使用来自 boost 的动态数组,并且无法为其编写适当的 getter 函数。这是我尝试过的(我检查了类 setter 中的数组大小以及 main 函数中的 getter):

#include <iostream>
#include "boost/multi_array.hpp"

using namespace std;
using namespace boost;

typedef multi_array<int, 3> array3i;

class Test {
private:
    array3i test_array_;

    void init(int x, int y, int z) {
        array3i test_array_(extents[x][y][z]);
        cout << "Size should be: " << test_array_.size() << endl;
        for (int j=0; j<x; j++) {
            for (int jj=0; jj<y; jj++) {
                for (int jjj=0; jjj<z; jjj++) {
                    test_array_[j][jj][jjj] = j+jj+jjj;
                }
            }
        }

    };

public:
    array3i test_array() {return test_array_;};

    Test(int x, int y, int z) {
        init(x, y, z);
    };
};

int main(int argc, const char * argv[]) {

    Test test(2,3,5);

    cout << "Size from getter: " << test.test_array().size() << endl;

    return 0;
}

【问题讨论】:

  • 您遇到的“麻烦”是什么? 如何它不起作用?请详细说明。

标签: c++ boost multidimensional-array


【解决方案1】:

在你的 init 函数中你应该有:

void init(int const x, int const y, int const z)
{
    //test_array_.resize(extents[x][y][z]);
    test_array_ = array3i(extents[x][y][x]); 
    cout << "Size should be: " << test_array_.size() << endl;
    for (int j = 0; j < x; j++) {
        for (int jj = 0; jj < y; jj++) {
            for (int jjj = 0; jjj < z; jjj++) {
                test_array_[j][jj][jjj] = j + jj + jjj;
            }
        }
    }
}

【讨论】:

  • 我的错,我忘记了一个下划线。
  • 不是这样的。你可以try it:core dumped
【解决方案2】:

问题可能是你有两个 test_array_ 变量:一个在类中作为类成员,一个本地init 函数中。 p>

局部变量隐藏并覆盖成员变量。

【讨论】:

    【解决方案3】:

    getter 正在工作,但您没有初始化成员。

        array3i test_array_(extents[x][y][z]);
    

    初始化一个局部变量(在init() 退出后不再存在)。

    有问题的部分是(可能)您不能只分配不同大小/形状的 multi_array。所以你需要使用resize()(或者在构造函数初始化列表中初始化test_array_形状)。

    Live On Coliru

    固定:

    #include <iostream>
    #include "boost/multi_array.hpp"
    
    using namespace std;
    using namespace boost;
    
    typedef multi_array<int, 3> array3i;
    
    class Test {
      private:
        array3i test_array_;
    
        void init(int const x, int const y, int const z)
        {
            test_array_.resize(extents[x][y][z]);
            cout << "Size should be: " << test_array_.size() << endl;
            for (int j = 0; j < x; j++) {
                for (int jj = 0; jj < y; jj++) {
                    for (int jjj = 0; jjj < z; jjj++) {
                        test_array_[j][jj][jjj] = j + jj + jjj;
                    }
                }
            }
        };
    
      public:
        array3i test_array() { return test_array_; };
    
        Test(int x, int y, int z) { init(x, y, z); };
    };
    
    int main()
    {
        Test test(2, 3, 5);
    
        cout << "Size from getter: " << test.test_array().size() << endl;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-08-21
      • 2016-03-16
      • 2018-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-01
      • 2023-04-02
      相关资源
      最近更新 更多