简体   繁体   English

使用运算符:: new和boost :: multi_array从A *存储路径

[英]Storing path from A*, using operator::new with boost::multi_array

I'm trying to use dynamic memory allocation for the first time. 我正在尝试第一次使用动态内存分配。 I want to allocate memory for a 2 dim dynamic array, to store paths from an A* function. 我想为2个暗淡的动态数组分配内存,以存储A *函数的路径。 I think the array for the job is boost::multi_array. 我认为工作的数组是boost :: multi_array。

Problem I seem to be able to allocate the memory but i can't change or access any of the elements. 问题我似乎能够分配内存,但是我无法更改或访问任何元素。

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

typedef boost::multi_array<int, 2> array_type;

int main()
{
    array_type *A = new array_type;

    A->resize( boost::extents[2][2] );

    A[1][1] = 2;

    std::cout << A[1][1] << std::endl;

    delete A;

    return 0;
}

Compiler says : 编译器说

C:\Coding\Code Projects\C++\Source Files\Console\main-read.cpp|14|error: no match for 'operator<<' in 'std::cout << boost::multi_array_ref<T, NumDims>::operator[](boost::multi_array_ref<T, NumDims>::index) [with T = int; unsigned int NumDims = 2u; boost::multi_array_ref<T, NumDims>::reference = boost::detail::multi_array::sub_array<int, 1u>; boost::multi_array_ref<T, NumDims>::index = int](1)'|.

I have tried so over ways of declaring A, but can't seem to find a solution. 我已经尝试过声明A的方式,但是似乎找不到解决方案。 any help would be appreciated. 任何帮助,将不胜感激。

The following solution works for me. 以下解决方案适用于我。 Sorry for the confusion before. 抱歉让您感到困惑。

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

typedef boost::multi_array<int, 2> array_type;

int main()
{
    array_type *A = new array_type(boost::extents[2][2]);
    (*A)[1][1] = 2;
    std::cout << (*A)[1][1] << std::endl;
    delete A;
    return 0;
}

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

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