Note that there are some explanatory texts on larger screens.

plurals
  1. POChoosing the data type for a generalized template class in main()
    primarykey
    data
    text
    <p>So I was asked to write a simple vector template and I believe I have written the class correctly, looking at some examples of generalized lists in our textbook (Savitch). Now I am trying to invoke the class in <code>main()</code> by letting the user choose the data type. I am running into problems after I declare the identifier <code>list1</code>. I want to be able to use the same identifier after using an if statement to switch the data type. However, I don't think the syntax in the body of my if statements is correct, since <code>list1</code> is already declared. In java I always thought after a class was declared you could call its construtor at any time, but I have no idea how to do that in C++. </p> <pre><code>#include &lt;iostream&gt; using namespace std; template &lt;class T&gt; class SimpleVector { public: SimpleVector(); SimpleVector(int); SimpleVector(const SimpleVector &amp; copy); ~SimpleVector(); int size(); T getElementAt(int n); T &amp; operator[](int index); private: T * item; int length; }; int main() { int dType; int dataSize; cout &lt;&lt; "What type of data do you want to enter?\n(1 for integer, 2 for double and 3 for strings)" &lt;&lt; endl; cin &gt;&gt; dType; cout &lt;&lt; "How many data inputs? " &lt;&lt; endl; cin &gt;&gt; dataSize; SimpleVector &lt;int&gt; list1; // if I dont declare then for loop doesn't recognize list as a declared variable. if (dType == 0) { SimpleVector &lt;int&gt; list1(dataSize); } else if (dType == 1) { SimpleVector &lt;double&gt; list1(dataSize); } else { SimpleVector &lt;string&gt; list1(dataSize); } cout &lt;&lt; "Please enter the data:" &lt;&lt; endl; for (int i = 0; i &lt; dataSize; i++) { cin &gt;&gt; list1[i]; } return 0; } template &lt;class T&gt; SimpleVector&lt;T&gt;::SimpleVector() { item = NULL; length = 0; } template &lt;class T&gt; SimpleVector&lt;T&gt;::SimpleVector(int s) { length = s; item = new T[length]; } template &lt;class T&gt; SimpleVector&lt;T&gt;::SimpleVector(const SimpleVector &amp; copy) { int newSize = copy - &gt; size(); item = new T[newSize]; for (int i = 0; i &lt; newSize; i++) item[i] = copy.item[i]; } template &lt;class T&gt; SimpleVector&lt;T&gt;::~SimpleVector() { delete[] item; } template &lt;class T&gt; int SimpleVector&lt;T&gt;::size() { return length; } template &lt;class T&gt; T SimpleVector&lt;T&gt;::getElementAt(int n) { return *(item + n); } template &lt;class T&gt; T &amp; SimpleVector&lt;T&gt;::operator[](int index) { return this-&gt;item[index]; } </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