简体   繁体   English

C ++访问指向结构的指针中的类数组指针

[英]C++ accessing class array pointer in a pointer to a structure

I am new to C++ and I really need some help on this.我是 C++ 的新手,我真的需要一些帮助。 I am trying to create a structure to interface with the GSL Monte-Carlo algorithms (a fact that is really not important for this example).我正在尝试创建一个结构来与 GSL Monte-Carlo 算法交互(这个事实对于这个例子来说并不重要)。 I have searched all of the C++ tutorials, the stackoverflow posts and the GSL documentation with no luck.我已经搜索了所有 C++ 教程、stackoverflow 帖子和 GSL 文档,但都没有运气。 I am using the armadillo package for matrix manipulation;我正在使用 armadillo 包进行矩阵操作; it is very robust.它非常坚固。 I am unable to use a dynamic array within the structure, as per the documentation, so I am trying to find a way to make my structure variable *M point to the values in my array *L[].根据文档,我无法在结构中使用动态数组,因此我试图找到一种方法使结构变量 *M 指向数组 *L[] 中的值。 I am sure that this would be better with a vector but 1) the rest of the code (in bad form) uses pointers already, and 2) I am looking at this as a learning experience.我确信使用向量会更好,但是 1) 其余代码(以错误的形式)已经使用了指针,并且 2)我将此视为一种学习经验。 I am surprised that the addresses for *M and *L[] are not the same in my code.我很惊讶 *M 和 *L[] 的地址在我的代码中不一样。 I am also, less importantly, surprised that my std::cout prints a different number of spaces for each line.我也不太重要的是,我的 std::cout 为每行打印不同数量的空格感到惊讶。 The code exits before printing the last std::cout as shown in the output below.代码在打印最后一个 std::cout 之前退出,如下面的输出所示。

Thanks for your help!谢谢你的帮助!

#include "pch.h"
#include "stdio.h"
#include "complex"
#include "new"
#include "armadillo"
using namespace arma;

class Link
{
public:

    arma::Mat<cx_double>::fixed<3, 3>* dir[4];                                      // pointer to directional SU(3) matrices

    Link();                                                                         // default constructor

};

Link::Link()                                                                        // default constructor - all directional matrices are the identity 
{

    for (size_t hcount = 0; hcount < 4; hcount++)
    {
        dir[hcount] = new arma::Mat<cx_double>::fixed<3, 3>{ fill::eye };           // create directional matrix in direction hcount
    }

}
struct Param
{
    Link* M;
};
int main()
{
    const int size  = 10;
    Param* Parameters = new Param{ NULL };
    Link* L[size];
    arma::Mat<cx_double>::fixed<3, 3> One{ fill::eye };


    for (size_t hcount = 0; hcount < 10; hcount++)
    {
        L[hcount] = new Link();
        *L[hcount]->dir[1] = *L[hcount]->dir[1] + hcount * One;                     // Make each array element #1 unique
    }

    Parameters->M = L[0];

    std::cout << "&L                        = " << &L << std::endl;
    std::cout << "&Parameters->M            = " << &Parameters->M << std::endl;                 // surprised that addresses are not the same
    std::cout << std::endl;

    std::cout << "&L[0]                     = " << &L[0] << std::endl;
    std::cout << "&Parameters->M[0]         = " << &Parameters->M[0] << std::endl;
    std::cout <<  std::endl;

    std::cout << "&L[5]                     = " << &L[5] << std::endl;
    std::cout << "&Parameters->M[5]         = " << &Parameters->M[5] << std::endl;
    std::cout << std::endl;

    std::cout << "&L[5]->dir[1]             = " << &L[5]->dir[1] << std::endl;
    std::cout << "&Parameters->M[5].dir[1]  = " << &Parameters->M[5].dir[1] << std::endl;
    std::cout << std::endl;

    std::cout << "*L[5]->dir[1]             = " << *L[5]->dir[1] << std::endl;                  // This works
    std::cout << "*Parameters->M[5].dir[1]  = " << *Parameters->M[5].dir[1] << std::endl;       // This does not
    std::cout << std::endl;

}
OUTPUT
&L                                              = 0024F7CC
&Parameters->M                  = 004EEFD8

&L[0]                                           = 0024F7CC
&Parameters->M[0]                       = 004E0578

&L[5]                                           = 0024F7E0
&Parameters->M[5]                       = 004E05C8

&L[5]->dir[1]                           = 004E50C4
&Parameters->M[5].dir[1]        = 004E05CC

*L[5]->dir[1]                           =     (+6.000e+00,+0.000e+00)                      (0,0)                      (0,0)
                      (0,0)    (+6.000e+00,+0.000e+00)                      (0,0)
                      (0,0)                      (0,0)    (+6.000e+00,+0.000e+00)

*Parameters->M[5].dir[1]        = 

&L is the adress of L , so it's the adress of the pointer to the first element not the adress of the first elemenr itself. &L是的ADRESS L ,所以它的指针的第一个元素不是第一elemenr本身的ADRESS的ADRESS。 Same for & Parameters->M .& Parameters->M相同。 That is the adress of thd the Member M from Parameters .那是来自Parameters的成员M的地址。 You want to compare L[0] with Parameters->M except when M should not point to the element that L[0] refers to but to the start of the array itself, then you want to compare it with L .您想将L[0]Parameters->M进行比较,除非 M 不应指向L[0]所指的元素而是指向数组本身的开头,然后您想将其与L进行比较。 But then you also have to change the assignment.但是,您还必须更改分配。

I find it a bit weird that you use an array of pointers.我觉得你使用指针数组有点奇怪。 Just use an array of Link s.只需使用一组Link

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

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