Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementing an array class, homework troubles
    primarykey
    data
    text
    <p>As the title says, one part of my assignment is to implement an array class. The professor gave us the header style to start off with, creating the function declarations for us. Using previous examples, I've tried my best to define these functions, but I'm having a lot of trouble understanding just what's going on. It might be a little much to post an entire header file, but I'll try to explain the issues I'm having regardless. Here is my code:</p> <pre><code>#ifndef ARRAY_H #define ARRAY_H #include &lt;iostream&gt; #include &lt;string&gt; using namespace std; template &lt;class T&gt; class Array { private: int size; T * arr; public: int getSize(); Array(); Array(int size); Array(Array &amp; other); Array(T[], int n); ~Array(); Array &amp; operator=(Array &amp; rhs); Array &amp; operator+(Array &amp; rhs); // append T &amp; operator[](int i); //allow read and write const T &amp; operator[](int n) const; // readonly void print(int n = 5); void print(ostream &amp; out, int n); operator int *() { return arr; } friend ostream &amp; operator &lt;&lt;(ostream &amp; out, Array &lt;T&gt;&amp; rhs); }; template &lt;class T&gt; int Array&lt;T&gt;::getSize() { return size; } template &lt;class T&gt; Array&lt;T&gt;::Array() { arr = new T[]; } template &lt;class T&gt; Array&lt;T&gt;::Array(int size) { arr = new T[size]; } template &lt;class T&gt; Array&lt;T&gt;::Array(Array &amp; other) { size = other.getSize(); arr = new T[size]; copy(arr[0], arr[size + 1], other.arr[0]); } template &lt;class T&gt; Array&lt;T&gt;::Array(T[], int n) { size = n; arr = new T[n]; } template &lt;class T&gt; Array&lt;T&gt;::~Array() { if (arr) { delete arr; } } template &lt;class T&gt; Array&lt;T&gt;&amp; Array&lt;T&gt;::operator=(Array &amp; rhs) { if (this != &amp;rhs) { delete[] arr; size = rhs.getSize(); arr = new T[size]; copy(arr[0], arr[size+1], rhs.arr[0]); } return *this; } template &lt;class T&gt; Array&lt;T&gt;&amp; Array&lt;T&gt;::operator+(Array &amp; rhs) { Array *tmp; tmp = new Array(size + rhs.getSize()); return *tmp; } 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 n) const { assert(0 &lt;= i &amp;&amp; i &lt; size); return arr[i]; } template &lt;class T&gt; void Array&lt;T&gt;::print(ostream &amp;out, int n) { for (size_t i = 0; i &lt; n; i++) { out &lt;&lt; arr[i] &lt;&lt; " "; } } template &lt;class T&gt; void Array&lt;T&gt;::print(int n = 5) { print(cout, n); } template &lt;class T&gt; ostream &amp; operator &lt;&lt; (ostream &amp; out, Array&lt;T&gt; &amp; rhs) { out &lt;&lt; "( " &lt;&lt; rhs.getSize() &lt;&lt; ")"; return out; } #endif </code></pre> <p>I'd appreciate any and all hints, just to help me figure out what's going on in just this class. I'm sorry if this ends up being a lot to ask for.</p> <p>Edit: Thank you all for the help again! The linker errors were fixed. I should've specified that print were the members of the class, instead of just being free.</p>
    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