0

I am writing a template Array class. I can use it to declare things like this,

Array<int> oneDimenional(5);

But not this...

Array<Array<Array<Array<Array<Array<Array< int >>>>>>> Craziness(1000);

My class starts out like this,

template <typename T>

class Array{

private:
    int len;
    T *arr;
public:
    Array() {
        int len = 0;
    }
    Array(int size) {
        arr = new T[size];
        len = size;
    }
    ~Array() {
        delete[] arr;
    }
//...
};

I'm guessing I need to change my constructor?

broinjc
  • 2,619
  • 4
  • 28
  • 44
  • 2
    One problem I can see is that in your constructor you do `int len = 0;` instead of `len = 0;`, i.e. you declare a new, local variable rather than setting the value of the member `len`. The compiler might issue a warning about that. – jogojapan Jun 01 '13 at 06:09
  • (Of course it would be even much better to use an initialization list: `Array() : len(0),arr(0) { }`.) – jogojapan Jun 01 '13 at 06:09
  • What you are asking is unclear. I get the impression that the question is how to pass all of the different dimensions to the internal types... if that is the case, you should also add what version of the standard you want to use, as it is simpl-ish in C++11 and much harder to do in C++03 – David Rodríguez - dribeas Jun 01 '13 at 07:15
  • Fixed! Thank you. Now I see my error is _actually_ with my copy constructor being used. (code not shown because I assumed the wrong error) – broinjc Jun 02 '13 at 19:05

1 Answers1

2
Array<Array<int> > arr(10);

leave space between >>. since this is considered as >> right shift. and thats why the error It 'll be shown in compiler itself and its a common mistake.

 error: '>>' should be '> >' within a nested template argument list

so your code should be

Array<Array<Array<Array<Array<Array<Array< int > > > > > > > Craziness(1000);
Dineshkumar
  • 4,165
  • 5
  • 29
  • 43
  • 3
    True, although it may be worth noting that this problem exists in C++98/03, but not in C++11. – jogojapan Jun 01 '13 at 06:14
  • @jogojapan could you please explain how? even i'm a beginner. [c++11 is new to me] – Dineshkumar Jun 01 '13 at 06:16
  • 1
    Oh, it's just that in C++11 the rules were changed. So you can write `Array>` without spaces and it will work. See this question as well: http://stackoverflow.com/questions/15785496/c-templates-angle-brackets-pitfall-what-is-the-c11-fix. But you are right nevertheless that this is likely the problem the OP faced. +1. – jogojapan Jun 01 '13 at 06:19
  • This is correct! I now see my driver program is actually doing that, Array< Array > b(3); (no ill-typed craziness) but I now see my error may actually be with a different piece of code which uses the copy constructor. – broinjc Jun 02 '13 at 19:04