Note that there are some explanatory texts on larger screens.

plurals
  1. POtemplates: parent class member variables not visible in inherited class
    primarykey
    data
    text
    <p>I have the following 4 files: </p> <ol> <li><code>arrayListType.h</code>: Declare and define <code>arrayListType</code> class as a template </li> <li><code>unorderedArrayListType.h</code>: Inherited from <code>arrayListType</code> class and Declares and defines <code>unorderedArrayListType</code> as a template. </li> <li><code>main1.cpp</code>: Test program to test <code>unorderedArrayListType</code> class. </li> <li><code>Makefile</code></li> </ol> <p>I get a compile error saying when accessing the protected variables of <code>arrayListType</code> in <code>unorderedArrayListType</code> for example: "length not declared in this scope", "list not declared in this scope", where length and list are protected variables in <code>arrayListType</code> class. </p> <p>The following are the codes:<br> arrayListType.h </p> <pre><code>#ifndef H_arrayListType #define H_arrayListType #include &lt;iostream&gt; using namespace std; template &lt;class elemType&gt; class arrayListType { public: const arrayListType&lt;elemType&gt;&amp;operator=(const arrayListType&lt;elemType&gt;&amp;); bool isEmpty() const; bool isFull() const; int listSize() const; int maxListSize() const; void print() const; bool isItemAtEqual(int location, const elemType&amp; item) const; virtual void insertAt(int location, const elemType&amp; insertItem) = 0; virtual void insertEnd(const elemType&amp; insertItem) = 0; void removeAt(int location); void retrieveAt(int location, elemType&amp; retItem) const; virtual void replaceAt(int location, const elemType&amp; repItem) = 0; void clearList(); virtual int seqSearch(const elemType&amp; searchItem) const; virtual void remove(const elemType&amp; removeItem) = 0; arrayListType(int size = 100); arrayListType(const arrayListType&lt;elemType&gt;&amp; otherList); virtual ~arrayListType(); protected: elemType *list; int length; int maxSize; }; template &lt;class elemType&gt; bool arrayListType&lt;elemType&gt;::isEmpty() const { return (length == 0); } // remaining non-virtual functions of arrayListType class #endif </code></pre> <p>unorderedArrayListType.h </p> <pre><code>#ifndef H_unorderedArrayListType #define H_unorderedArrayListType //#include &lt;iostream&gt; #include "arrayListType.h" //using namespace std; template &lt;class elemType&gt; class unorderedArrayListType: public arrayListType&lt;elemType&gt; { public: void insertAt(int location, const elemType&amp; insertItem); void insertEnd(const elemType&amp; insertItem); void replaceAt(int location, const elemType&amp; repItem); int seqSearch(const elemType&amp; searchItem) const; void remove(const elemType&amp; removeItem); unorderedArrayListType(int size = 100); }; template &lt;class elemType&gt; void unorderedArrayListType&lt;elemType&gt;::insertAt(int location, const elemType&amp; insertItem) { for(int i = length; i &gt; location; i--) list[i] = list[i - 1]; list[location] = insertItem; length++; } // Remaining virtual functions that need to be defined by the inherited class #endif </code></pre> <p>main1.cpp </p> <pre><code>#include &lt;iostream&gt; #include "unorderedArrayListType.h" using namespace std; int main() { unorderedArrayListType&lt;int&gt; intList(25); int number; cout&lt;&lt;"Line 3: Enter 8 integers: "; for(int count = 0; count &lt; 8; count++) { cin&gt;&gt;number; intList.insertEnd(number); } cout&lt;&lt;"Line 8: intList: "; intList.print(); cout&lt;&lt;endl; } </code></pre> <p>Makefile: </p> <pre><code>all: main1 main1.o: main1.cpp g++ -c -Wall main1.cpp main1: main1.o g++ -Wall main1.o -o main clean: rm -f *.o *~ main1 </code></pre> <p>The following is the compilation error: </p> <pre><code>make g++ -c -Wall main1.cpp In file included from main1.cpp:2: unorderedArrayListType.h: In member function 'void unorderedArrayListType&lt;elemType&gt;::insertAt(int, const elemType&amp;)': unorderedArrayListType.h:30: error: 'length' was not declared in this scope unorderedArrayListType.h:31: error: 'list' was not declared in this scope unorderedArrayListType.h:33: error: 'list' was not declared in this scope </code></pre> <p>More functions of <code>unorderedArrayListType</code> listed and protected variables indicated as not declared in the scope. Wondering what could be the error. </p> <p>New error: </p> <pre><code>make g++ -Wall main1.o -o main Undefined first referenced symbol in file arrayListType&lt;int&gt;::seqSearch(int const&amp;) constmain1.o ld: fatal: Symbol referencing errors. No output written to main collect2: ld returned 1 exit status *** Error code 1 make: Fatal error: Command failed for target `main1' </code></pre>
    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.
 

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