简体   繁体   English

如何在C ++中的结构中初始化向量

[英]How to initialize a vector in a struct in c++

I am having a problem with vectors in c++. 我在C ++中遇到向量问题。 I am new to c++ so keep this in mind please. 我是C ++的新手,所以请记住这一点。

So I have the following struct in the begining of my program: 因此,在程序开始时我具有以下结构:

typedef struct grupo
{
    float transX, transY, transZ, rotX, rotY, rotZ, rotAngle, scaleX, scaleY, scaleZ;
    char **modelos;
    struct grupo** grupos;
    int nNomes = 0, nGrupos=0;
    std::vector<float> vertices;
};

struct grupo *grupo;

And I just start the main like this: 我只是这样开始主程序:

grupo = (struct grupo*)malloc(sizeof(struct grupo));
grupo->modelos = (char **)malloc(sizeof(1));
grupo->grupos = (struct grupo **)malloc(sizeof(struct grupo));

And in the middle of main I am tryting to use this: 在main的中间,我尝试使用它:

grupo->vertices.push_back(x);

But it keeps crashing the program. 但是它总是使程序崩溃。 I am using visual studio, and using debugging mode. 我正在使用Visual Studio,并使用调试模式。 I can see that is when pushing X to the vector that crashes (x has a value). 我可以看到,这是当将X推到崩溃的向量上时(x有一个值)。 I've tried to change the vector to an array of floats. 我试图将向量更改为浮点数数组。 So the problem i can imagine is with the initialization of the vector... Can you help with what am I missing? 所以我可以想象的问题是向量的初始化...您能帮我解决一下吗?

Thank you :) 谢谢 :)

You're using C++, you don't need the "new" or malloc keyword unless for specific reasons. 您使用的是C ++,除非出于特殊原因,否则不需要“ new”或malloc关键字。 You don't need the typedef for structs. 您不需要为结构体使用typedef。

I'm not sure what your char** is supposed to be, but you can use std::string for strings. 我不确定您的char **应该是什么,但是您可以将std :: string用于字符串。

Maybe what you're trying to do is this: 也许您想要做的是这样的:

struct Grupo
{
    float transX, transY, transZ, rotX, rotY, rotZ, rotAngle, scaleX, scaleY, scaleZ;
    std::string modelos;
    int nNomes = 0, nGrupos=0;
    std::vector<float> vertices;
};

In main: 在主要方面:

Grupo grupo;
grupo.vertices.push_back( ... );

Then I would advise you to read a bit more about what is exactly the C++ language and how it's not C. 然后,我建议您多读一些关于什么是C ++语言以及它不是C语言的更多信息。

Since you're using C++; 由于您使用的是C ++; if you want to create a grupo dynamically, you should use the new operator, with the constructor: 如果你想创建一个grupo动态,您应该使用new运营商,拥有的构造器:

grupo = new grupo();

malloc does not properly initialize C++ objects such as std::vector . malloc不能正确初始化C ++对象,例如std::vector


PS I am not sure what the grupo->modelos and grupo->grupos are supposed to be, but I'd use proper C++ types for them (perhaps modelos should be std::string , etc). PS我不确定grupo->modelosgrupo->grupos应该是什么,但是我会为它们使用适当的C ++类型(也许modelos应该是std::string等)。 Additionally, I suspect that you've got one * too much for both modelos and grupos . 此外,我怀疑你有一个*吃不消都modelosgrupos

C++ does not need the typedef in the declaration. C ++在声明中不需要typedef。

To properly initialize a structure, you should write a ctor to replace the compiler provided ctor (which generally does nothing). 要正确初始化结构,您应该编写一个ctor来替换编译器提供的ctor(通常不执行任何操作)。

Something like the following (with just a few attributes): 类似于以下内容(仅带有一些属性):

struct grupo
{
   float transX, transY;
   // ...
   int nNomes;
   int nGrupos;
   std::vector<float> vertices;

   // I prefer initialization list form
   grupo() : transX(0.0),
             transY(1.0),
             // ...
             nNomes(0),
             nGrupos(0)
             // vertices default ctor is ok, creates empty vector
      {
         // use vertices.push_back(...); to fill vertices
      }

};

grupo grupo;

Next you will want to write a more useful ctor, one with parameters to use (instead of the literal constants), such that you could build multiple grupo. 接下来,您将要编写一个更有用的ctor,一个带有要使用的参数(而不是文字常量)的ctor,以便您可以构建多个grupo。

grupo grupo1(1.0, 2.0, 3, 4);
grupo grupo2(3.0, 4.0, 5, 6);
// etc.

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

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