Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <ol> <li>When trying to do a copy constructor for <code>Array(Array &amp; other);</code> on line 46, I get the error <code>error C2664: 'char *strcpy(char *,const char *)' : cannot convert argument 1 from 'int *' to 'char *'</code> It keeps pointing to the <code>strcpy</code> function, but I'm not sure what it's asking me to change. I get two errors for the same line, the second of which says <code>cannot convert argument 1 from 'std::string *' to 'char *'</code></li> </ol> </blockquote> <p>The problem here is that <code>strcpy</code> is meant to copy <code>char</code> types between two memory locations. However, your template argument <code>T</code> could be any type really and so use of <code>strcpy</code> here is rather inappropriate if you're going for genericity. A straight-forward(but not very efficient) implementation for your copy constructor would be something like:</p> <pre><code>template &lt;class T&gt; Array&lt;T&gt;::Array(const Array &amp;other) : size(other.size), arr(new T[size]) { for (int i = 0; i &lt; size; ++i) arr[i] = other.arr[i]; } </code></pre> <blockquote> <p>2) I get another error with trying to <code>strcpy</code> function on line 71 when writing the <code>operator=</code> overloader. It tells me <code>cannot convert argument 1 from 'std::string *' to 'char *'</code></p> </blockquote> <p>Again, it's the same problem as one: older C-style <code>str*</code> functions work with raw <code>char *</code> pointers to some chunk of memory containing characters. As such, these groups of functions cannot directly work with <code>std::string</code> from the standard c++ library without some adaptation. Mostly of the time you don't want to do this because those functions can mutate <code>std::string</code> without preserving its internal invariants which is dangerous.</p> <p>For the assignment operator, you can just do a copy and swap:</p> <pre><code>template &lt;class T&gt; Array&lt;T&gt;&amp; Array&lt;T&gt;::operator = (Array rhs) { std::swap(size, rhs.size); std::swap(arr, rhs.arr); return *this; } </code></pre> <blockquote> <p>3) For the <code>T &amp; operator[](int i); //allow read and write</code> on line 85, I'm wondering if I'm supposed to create a new array with <code>i</code> slots. The allow, read, and write comment the professor left confused me. Also, for the following function, <code>Array&lt;T&gt;::T &amp; operator[] (int n) const;</code> the comment said read only, so I assumed I was just supposed to return the array with <code>n</code> items.</p> </blockquote> <p>The <code>operator []</code> is for providing syntactic sugar for accessing a specific item in your <code>Array&lt;T&gt;</code> given some index <code>i</code>. Without it the following wouldn't work:</p> <pre><code>Array&lt;int&gt; arr(42); std::cout &lt;&lt; arr[0]; </code></pre> <p>As such, you shouldn't have to allocate anything inside this operator. You can implement both const and non-const version in the same way. Only difference is one will have a <code>const</code> qualifier at the end so this will work with <code>const Array&lt;T&gt;</code> also:</p> <pre><code>template &lt;class T&gt; T&amp; Array&lt;T&gt;::operator[] (int i) { assert(0 &lt;= i &amp;&amp; i &lt; size); return arr[i]; } template &lt;class T&gt; const T&amp; Array&lt;T&gt;::operator[] (int i) const { assert(0 &lt;= i &amp;&amp; i &lt; size); return arr[i]; } </code></pre> <blockquote> <p>4) I left a comment for the <code>void print(int n = 5);</code> function. But to summarize, I want to be able to print out a certain number of items in the array per line, but there's no multidimensional array. I attempted two <code>for</code> loops, but it just got stuck on the same array item.</p> <p>5) I'm not sure why <code>void print(ostream &amp; out, int n);</code> is needed. Is it supposed to override the above print statement?</p> </blockquote> <p>The proper terminology you're looking for is "overloading". "overriding" has a very specific meaning in C++ nomenclature which doesn't apply in this scenario.</p> <p>Judging from the declaration, it seems the intent for the second <code>print</code> signature is to support different kinds of output stream (of which <code>cout</code> is one type btw). What you'd want to do here is to implement the first version of <code>print</code> in terms of the second one. In fact, they don't even need to be template functions at all:</p> <pre><code>void print(ostream &amp;out, int n) { for (size_t i = 0; i &lt; n; ++i) out &lt;&lt; pets[i] &lt;&lt; " "; } void print(int n) { print(cout, n); } </code></pre> <p>Note, <code>print</code> should probably be modified to take the array to print out rather than referring to one (global?) particular instance here.</p> <p><strong>Edit:</strong> When modifying <code>print</code> to take the array to print it will of course have to be a template. One possible signature:</p> <pre><code> template &lt;typename T&gt; void print(const Array&lt;T&gt; &amp;arr, int n) { print(arr, n, cout); } </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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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