Note that there are some explanatory texts on larger screens.

plurals
  1. POthrow not working properly
    primarykey
    data
    text
    <p>I declared the size of the array to be 2 and I tried inserting on the 3rd array position. The insert function is supposed to throw -1 when the position > size+1(using pos-1 when assigning the element into the array) but the throw is not stopping the assignment for the 3rd array position so I am getting a segmentation fault.</p> <pre><code>template &lt;class T, int N&gt; class person : public people&lt;T&gt; { private: T a[N]; int size; public: person(); virtual void insert(int pos, T info); virtual T show(int pos); }; template&lt;class T, int N&gt; person&lt;T,N&gt;::person(){ size = 0; } template &lt;class T, int N&gt; void person&lt;T,N&gt;::insert(int pos, T info){ if (pos &lt;= 0 || pos &gt; size+1) throw -1; a[pos-1] = info; ++size; } template &lt;class T, int N&gt; T person&lt;T,N&gt;::show(int pos){ if ( pos &lt;= 0 || pos &gt; size+1 ) throw -1; return a[pos-1]; } void putin( people&lt;name&gt;*&amp; aPerson ) {//passing aPerson itself string first("Julia"), last("Robert"); name temp(first, last); string ft("Delilah"), lt("McLuvin"); name temp2(ft, lt); string fst("oooh lala"), lst("broomdat"); name temp3(fst, lst); try{ aPerson-&gt; insert(1,temp); aPerson-&gt; insert(2,temp2); aPerson-&gt; insert(3,temp3); } catch(...){ cout&lt;&lt; "error\n";} } int main(){ people&lt;name&gt;* aPerson = new person&lt;name, 2&gt;(); putin(aPerson); try{ cout &lt;&lt; aPerson-&gt;show(1); cout &lt;&lt; aPerson-&gt;show(2); cout &lt;&lt; aPerson-&gt;show(3); } catch(...){ cout&lt;&lt; "error\n";} return 0; } </code></pre>
    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