Note that there are some explanatory texts on larger screens.

plurals
  1. POPointer is not initialized?
    primarykey
    data
    text
    <p>In my main class, I have a person object, which, in the person class, contains a pointer to a Strategy object. I'm trying to do this:</p> <pre><code>person.strategyPointer-&gt;getStrategyType() </code></pre> <p>But when I print this out, I get a special number, 3435973836. This probably means my strategyPointer has not been initialized.</p> <p>In my Person class header file, I declare this:</p> <pre><code>Strategy* strategyPointer; </code></pre> <p>And in the Person.cpp file I initialize the pointer like this.</p> <pre><code>Person::Person() : strategyPointer(nullptr) { } </code></pre> <p>If I point the pointer to null, why is the compiler still complaining that I don't have the strategyPointer initialized? Or perhaps since it points to null, it falls out of scope and gets deleted? I actually point the pointer to something when I'm parsing. I do something like this:</p> <pre><code>StrategyType newStrat; newStrat.setStrategyType(strategyType); person.strategyPointer = &amp;newStrat </code></pre> <p>I'm using inheritance, and Strategy is my base class. However, it complains about initialization later on back in my main source file. I have no idea what's going on with this initialization problem. I can't point the pointer to a bogus Strategy object either.</p> <p>EDIT: I have just added a new variable declaration in my Person header file, Strategy newStrategy; I later assign the StrategyType object which I've created into newStrategy, and I point strategyPointer to &amp;newStrategy. This should prevent the StrategyType object that I've created from falling out of scope, right? But in my main.cpp I still get the same value when I try to print out person.stratPointer->getStrategyType(); However, if I print out person.newStrategy.getStrategyType(), I get the value which I want.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload