简体   繁体   English

向量在构造函数中的初始化-C ++

[英]Initialization of vector in a constructor - C++

I'm struggling with the constructor of one of my classes do to a member that is not initialized properly. 我正在努力与我的类之一的构造函数做一个未正确初始化的成员。

I have a class "Settings" that handles the setting I use for my simulations and a class Simulations that performs the simulation steps. 我有一个“ Settings”类来处理我用于模拟的设置,还有一个“ Simulations”类来执行模拟步骤。

What I can't understand is why this code doesn't work as expected: 我不明白的是为什么这段代码无法按预期工作:

      class Settings{ 
         public: 
           int n ; // a number I need to create properly a vector in my class simulation
           // ... rest of the code constructors etc to read values from files.
           // everything works fine and the values are assigned properly
       }

       class Simulation{
          public:
          std::vector<int> v ;
          Settings *SP;

          Simulation(Settings *);
        }

        Simulation::Simulation(Settings *pS) 
           :SP(pS), v(std::vector<int>(SP->n,0)) {}    // the constructor doesn't work,
    // v is initialized but it is not created as a vector of size n, but 0.

I think there is a problem in the way I use the constructor but I can't understand why. 我认为使用构造函数的方式存在问题,但我不明白为什么。

By the way defining v inside the curly brackets works fine, I'm just curious to know why defining it the proper way doesn't work as expected! 顺便说一下,在大括号内定义v可以正常工作,我只是想知道为什么以正确的方式定义它不能按预期工作!

Thanks a lot for the help! 非常感谢您的帮助!

You've verified that pS->n != 0 prior to instantiating the Simulation , right? 您已经在实例化Simulation之前验证了pS->n != 0 ,对吗?

Anyway, I think the line you're looking for in your constructor is: 无论如何,我认为您要在构造函数中查找的行是:

:SP(pS), v(pS->n, 0) {}

The way you're doing it now is creating a whole std::vector and then copying it to v . 现在,您要做的就是创建一个完整的std::vector ,然后复制v

You don't need the extra vector: 您不需要额外的向量:

Simulation::Simulation(Settings *pS) 
       :SP(pS), v(SP->n,0) {}

If this doesn't work, this isn't your code. 如果这不起作用,那么这不是您的代码。 Are you sure SP is declared before v in the class definition? 您确定在类定义中的v之前声明了SP吗? If this also doesn't work, try with pS instead of SP . 如果仍然pS ,请尝试使用pS而不是SP

Also please make sure you check SP is not null pointer. 另外,请确保您检查SP不是空指针。 This will otherwise have a crash. 否则将导致崩溃。

Simulation::Simulation(Settings *pS) 
   :SP(pS), v(pS != NULL ? pS->n : 0 , 0) {}

This will check for SP not being NULL. 这将检查SP是否不为NULL。 this is the case when Simulation(NULL) is used as constructor. 当Simulation(NULL)用作构造函数时就是这种情况。

You don't need to create an extra vector and use the copy constructor. 您无需创建额外的向量并使用复制构造函数。 Just pass the arguments straight to your vector in the member initializer. 只需将参数直接传递给成员初始化器中的向量即可。 As another poster mentioned, did you verify that the return of SP->n is actually not 0? 就像另外一个提到的那样,您是否验证了SP-> n的返回实际上不是0? If you hardcode some values in, you'll see that it works fine, as below: 如果您对其中的一些值进行硬编码,您会发现它工作正常,如下所示:

#include <iostream>
#include <vector>
using namespace std;

class foo
{
public:
    foo();
    vector<int> vec;
};

int main()
{
    foo obj;
    for(int i=0;i<obj.vec.size();++i) {
        cout << obj.vec[i] << ' ';
    }
    system("pause");
    return 0;
}


foo::foo()
    :vec(vector<int>(10,2))
{

}

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

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