Note that there are some explanatory texts on larger screens.

plurals
  1. POiterating through pointer arrays and displaying
    primarykey
    data
    text
    <p>ii'm having some issues understanding pointer arrays, see the following simplified code</p> <pre><code>#include &lt;string&gt; #include &lt;iostream&gt; using namespace std; //holds two different datatypes template &lt;typename A, typename B&gt; class data { public: A dataA; B dataB; data(){} data* add(A dataA, B dataB){ this-&gt;dataA = dataA; this-&gt;dataB = dataB; return this; } }; //stores an array of the data template &lt;typename A, typename B&gt; class container { data&lt;A, B&gt; **dataArray; int ammount; public: typedef data&lt;A, B&gt; * iterator; container(){ //create dynamic array of pointers dataArray = new data&lt;A, B&gt;*[5]; ammount = 0; } ~container(){ for (int i = 0; i &lt;= ammount; i++) { delete dataArray[i]; } } void add(A dataA, B dataB){ data&lt;A, B&gt; *newItem; if (ammount &lt;= 5){ newItem = new data&lt;A, B&gt;(); dataArray[ammount] = newItem-&gt;add(dataA, dataB); ammount++; } } //basic iterators iterator begin(){ return *dataArray; } iterator end(){ return dataArray[5]; } }; void main(){ string somestuff[] = { "one", "two", "three", "four", "five" }; container&lt;string, int&gt; foo; for (int i = 0; i &lt; 5; i++){ foo.add(somestuff[i], i); } container&lt;string, int&gt;::iterator it = foo.begin(); // why cant i just iterate through each item in the array for (it; it &lt; foo.end(); it++) { cout &lt;&lt; "Data A : " &lt;&lt; it-&gt;dataA &lt;&lt; " Data B : " &lt;&lt; it-&gt;dataB &lt;&lt; "\n"; } } </code></pre> <p>the issue is in main, I cant move through the array when displaying. Am I looking at this problem all wrong ?</p> <p>ps I am aware STL containers would be better here but I need to understand this specific scenario, i.e. a dynamic array(not using vector) of pointers to objects</p> <p>**edit i have added a few output statements to show what I am trying to achieve</p> <p>output is </p> <pre><code>Adding data Added Data at address : 00779218 Added Data at address : 00779458 Added Data at address : 007794B8 Added Data at address : 00779560 Added Data at address : 00779730 Printing data The address of 'it' is now: 00779218 Data A : one Data B : 0 incerementing from map.begin The address of 'it' is now: 00779238 Data A : Data B : 469784212 incerementing from map.begin The address of 'it' is now: 00779258 Data A : X Data B : -33686019 incerementing from map.begin The address of 'it' is now: 00779278 </code></pre> <p>when i am doing this </p> <pre><code>for (it; it &lt; foo.end(); it++){ cout &lt;&lt; "The address of 'it' is now: "&lt;&lt;it&lt;&lt;"\n"; cout &lt;&lt; "Data A : " &lt;&lt; it-&gt;dataA &lt;&lt; " Data B : " &lt;&lt; it-&gt;dataB &lt;&lt; "\n"; cout &lt;&lt; "incerementing from map.begin\n"; } </code></pre> <p>I need the 'it' variable top point at the next item in the dataArray, it is incrementing just not properly</p>
    singulars
    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.
    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