Note that there are some explanatory texts on larger screens.

plurals
  1. POusing -> to access a virtual function from an array of objects indices
    primarykey
    data
    text
    <p>I am currently coding a c++ assignment focusing on the properties of inheritance.</p> <p>There is a base <strong>class gun {}</strong>, and several derived classes:</p> <p><strong>handgun: public gun {}</strong></p> <p><strong>revolver: public handgun{}</strong></p> <p><strong>pistol: public handgun{}</strong></p> <p>As per the current assignment, I have added a <strong>virtual</strong> function <strong>print()</strong> to each of these classes that overrides the previous class's <strong>print()</strong>, adding its unique variables to be printed. For example, gun has <strong>string manufacturer</strong>, handgun has <strong>string grips</strong>, and pistol has <strong>bool semiauto</strong>.</p> <p>Now, my professor wants me to make an array containing 2(two) objects, one object of type <strong>revolver: public handgun</strong> and one object of type <strong>pistol: public handgun</strong>. </p> <p>In the assignment, he says we MUST use this exact code to print out the contents of the array:</p> <pre><code>for(int i=0; i &lt;= numGuns; i++) **gunCabinet[i]-&gt;print();** </code></pre> <p>Now, for my code. The error in question is in bold, right near the bottom.</p> <pre><code>int main() { //declares the revolver and pistol objects revolver Anaconda; pistol M9; //sets appropriate variables for the objects Anaconda.setManufacturer("Smith &amp; Wesson"); M9.setManufacturer("Beretta"); Anaconda.setCaliber(".44 special"); M9.setCaliber("9x19mm Parabellum"); Anaconda.setGrips("Hogue grips"); M9.setGrips("U.S. Military specification grips"); Anaconda.setSights("6x scope"); M9.setSights("U.S. Military specification iron sights"); Anaconda.setLaser(true); M9.setLaser(false); Anaconda.setSingleAction(true); Anaconda.setNumberOfRounds(6); M9.setSemiAuto(true); int numGuns = 2; gun* gunCabinet; gunCabinet = new gun[numGuns]; gunCabinet[0] = Anaconda; gunCabinet[1] = M9; // *(gunCabinet) = Anaconda; //*(gunCabinet + 1) = M9; //gun * gunCabinet[/*numGuns*/2] = {&amp;Anaconda, &amp;M9}; for(int i=0; i &lt;= numGuns; i++) **gunCabinet[i]-&gt;print(); //ERROR ON THIS LINE** //cout &lt;&lt; Anaconda &lt;&lt; endl; //Anaconda.print(); //cout &lt;&lt; M9 &lt;&lt; endl; //M9.print(); return 0; } </code></pre> <p>I have commented out my other attempts at making this work.</p> <p>Now, when I run this, I get an error saying "base operand of ‘->’ has non-pointer type ‘gun’".</p> <p>Unless my professor made a mistake, I assume it is all me on this one. The problem is, I can't figure out what I am doing wrong. I am not necessarily looking for an answer to my homework, rather, is his code valid? And if it is valid, can I have some pointers (no pun intended) on what I am doing wrong? thanks a lot!</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.
    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