0

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.

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!

Thanks a lot for the help!

1
  • Short, complete, compileable example that demonstrates the problem, please. Commented Aug 6, 2012 at 3:02

4 Answers 4

2

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? If this also doesn't work, try with pS instead of SP.

Sign up to request clarification or add additional context in comments.

3 Comments

you were right, SP was not declared before v! I thought that the ordered that mattered was the one in the constructor! Thanks for the advice, it now works fine :)
@LucaCerone members are intialized in the order in which they appear in the class definition, that's why the correct size wasn't taken ;)
Yes, I realized that later.. in the question I wrote it ok, but now I have changed it so that people can realize where my mistake was!
2

You've verified that pS->n != 0 prior to instantiating the Simulation, right?

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.

Comments

0

Also please make sure you check SP is not null pointer. 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. this is the case when Simulation(NULL) is used as constructor.

Comments

0

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? 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)) { } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.