1

I need to create a template because I don't know what it's an array of. And it needs to be of the size that's passed in the constructor. So here is what I got and I got all sorts of errors. I am a beginner to C++ so any help is appreciated :)

template <typename T, int N>
class Array
{
  public:
    T& operator[](int index)
    {
      return data[index];
    }
  private:
    int size;
    T *data[N];
};

I think you understand what I'm trying to do. I also need to overload the subscript operator, as you can see. Not sure if I need a reference or a pointer or what. I did have a constructor but it wasn't working properly.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
Peter Griffin
  • 749
  • 9
  • 14

2 Answers2

0

Here's a corrected version with a sample main as well:

#include <iostream>
using namespace std;

template <typename T, int N>
class Array
{
  public:
    T& operator[](int index)
    {
      // add check for array index out of bounds i.e. access within 0 to N-1
      return data[index];
    }
    Array() {
        data = new T[size = N];
    }
    ~Array() {
        if (data)
            delete [] data;
    }
  private:
    int size;
    T *data;
};

int main(void) {
    Array<int, 4> a;
    a[0] = 5;
    cout << a[0] << endl;
    return 0;
}
Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69
  • Thank you very much. I understand the concepts well. I just have a hard time in the C++ language. The syntax gets me. Your example makes the implementation in C++ much more clear to me. – Peter Griffin Jul 15 '14 at 09:01
  • The whole point of passing the size as a template parameter is to avoid using dynamic allocation. – T.C. Jul 15 '14 at 16:16
-1

From what I can see your template is containing an array of pointers, which from what I read and from your [] operator implementation is not what you intend to do. So first you should remove the * from :

T *data[N];

You should probably initialize the size of your template from your constructor, thus you should change :

T *data[N];

to :

T* data;

and :

template <typename T, int N>

to :

template<typename T>

Now that changes the implementation a bit, you should now write a constructor like that :

template<typename T>
Array(int n) {
    data = new T[n];
}

and now you should also add a destructor like that :

~Array() {
    delete[] data;
}

and there you go :)

However if you want to keep the size as an argument of the template your constructor go like :

template<typename T, int N>
Array() {
}

and the declaration of data :

T data[N];

As said in comments, you may want to use std::array in "real life" conditions, but as a training, implementing your own Array is a good thing to do, that's a training most computer schools do after all.

Jeremy B.
  • 73
  • 9
  • Templates can have non-type parameters. – T.C. Jul 15 '14 at 07:44
  • And what issues exactly ? I do want to enhance my answer if it has issues but if you don't say what they are how could I ? Plus this is one of the most standard ways to do Arrays. I answered while keeping in mind his choices (array with a pointer and int as template argument) and I adviced him to do differently to fit the C++ standard way of doing. If he wants to make his own implementations of arrays to train, he should. Implementing basic versions of standard or STL containers is a good training ;) – Jeremy B. Jul 15 '14 at 07:58
  • @DevilBlackDeath First of all one should not use `new` `delete` directly in c++, second the sample doesn't implement the [Rule of Three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) properly. Also I don't see a point to create the `T` array with `new` if the size is fixed using the template parameter `N`. – πάντα ῥεῖ Jul 15 '14 at 09:09
  • True about your last point. However the [Rule of Three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) is of the author's responsibility, he asked about why it didn't work and how to make it work. And why exactly should new and delete be avoided? (`malloc` and `free` should be avoided however) I implemented smart pointers in C++03 and I don't really see how I could have done without `new` `delete` – Jeremy B. Jul 15 '14 at 09:20
  • @DevilBlackDeath If `T data[N];` is used the sentence _'However the destructor are still the same as my initial solution !'_ is certainly wrong. `delete[]` isn't needed in this case. – πάντα ῥεῖ Jul 15 '14 at 09:43
  • Thanks, did so many edits at once that I lost track of what I said ;) Shouldn't the poster accept Debasish's answer? :S – Jeremy B. Jul 15 '14 at 12:28