无法将一个3d矩阵转换为另一个

时间:2013-09-10 02:18:57

标签: c++ c++11 vector unordered-map

我将函数SARavesphnp()定义为类型:

std::unordered_map<unsigned, std::unordered_map<unsigned, std::unordered_map<unsigned, double>>>;

但是,当我尝试返回相同类型的变量时,我收到此错误:

error: could not convert '(mat3d (*)[(long int)y2][(long int)z2])(& SARave)' from 'mat3d (*)[(long int)y2][(long int)z2] {aka std::unordered_map<unsigned int, std::unordered_map<unsigned int, std::unordered_map<unsigned int, double> > > (*)[(long int)y2][(long int)z2]}' to 'mat3d {aka std::unordered_map<unsigned int, std::unordered_map<unsigned int, std::unordered_map<unsigned int, double> > >}'

这是我的代码:

#include <unordered_map>

using mat3d = std::unordered_map<unsigned, std::unordered_map<unsigned, std::unordered_map<unsigned, double>>>;

mat3d foo(const int, const int, const int);

int main()
{

    foo(10, 10, 10);

    return 0;
}

mat3d foo(const int x2, const int y2, const int z2)
{
    mat3d SARave[x2][y2][z2];

    return SARave;
}

1 个答案:

答案 0 :(得分:1)

试试这个。在gcc-4.7下为我工作。

mat3d foo(const int x2, const int y2, const int z2)
{
    mat3d SARave;
    SARave[x2][y2][z2] = 0.;

    return SARave;
}

这些类型没有任何问题。但是,这一行mat3d SARave[x2][y2][z2];没有初始化变量,而是声明了一个返回mat3d的函数。对不起,我现在无法解释原因,但错误消息中的»mat3d (*)[(long ...部分表明编译器试图将函数指针转换为mat3d对象,这显然会失败。