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?

4
  • 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. Commented Jun 1, 2013 at 6:09
  • (Of course it would be even much better to use an initialization list: Array() : len(0),arr(0) { }.) Commented Jun 1, 2013 at 6: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 Commented Jun 1, 2013 at 7: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) Commented Jun 2, 2013 at 19:05

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

4 Comments

True, although it may be worth noting that this problem exists in C++98/03, but not in C++11.
@jogojapan could you please explain how? even i'm a beginner. [c++11 is new to me]
Oh, it's just that in C++11 the rules were changed. So you can write Array<Array<int>> without spaces and it will work. See this question as well: stackoverflow.com/questions/15785496/…. But you are right nevertheless that this is likely the problem the OP faced. +1.
This is correct! I now see my driver program is actually doing that, Array< Array<int> > 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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.