1

i have tried to create a class template array that would accept accept arbitrary boundaries like 50 to 60, 100 to 200

I am new to programming and I have a layman question whether templates have scope. I tried to compile the program below

#include <iostream>
#include <vector>
#include <exception>

using namespace std;

template<typename t>
class array
{
private:
    size_t size;
    int lowerbound, upperbound;
    vector<t> data;
public:
    array(int , int );
    array(const array<t> &);
    int operator [](int );
};
template<typename t>
array<t>::array(int lbound, int rbound): 
    lowerbound(lbound), upperbound(rbound), size(upperbound - lowerbound + 1), data(size)
{

}

array<t>::array(const array<t> & c): 
    lowerbound(c.lowerbound), upperbound(c.upperbound), size(c.size)
{
    data.reserve(size);
}

int array<t>::operator[](int index)
{
    if (index < lowerbound || index > upperbound)cout << "fuck" << endl;
    else
        return data[index - lowerbound];
}

in copy constructor array<T>::array(const array <T> &C) it says

t is undeclared..

I thought as I already declared template <typename t> at very start there is no need for additional template<typename t>

Here are the questions I have

  1. Do templates have scope or scope like thing

  2. If so, while defining member functions of class template outside class scope we have to declare so many template <typename t> definitions or is there any better alternative

  3. This might be unrelated to templates; how do I specify size of vector or array at runtime like variable length arrays (VLAs) in c99. For example v.reseve(n) where n is known at runtime.

Praetorian
  • 106,671
  • 19
  • 240
  • 328
virusai
  • 27
  • 1
  • 7
  • Probably you need to add a `template` before the definition of your copy constructor. It's a little hard to tell from your question, because you didn't really paste the exact error message. You should try to paste the exact error message, on a different line from the rest of your question so it is easier to see – 1800 INFORMATION Jun 02 '15 at 06:17
  • Fyi, questionable name for your template class, made worse by the likelihood of one or more included standard headers pulling in ``, which wouldn't be bad unless you also `using namespace std;` (which ... you did). Also, your member initializer list uses `upperbound` and `lowerbound` in computing `size` prior to their own initialization. Members are initialized in *declaration* order in the class; not appearance order in the member-init list. And the missing `template` parameter decls are mandatory when implementing out-of template-class member function implementations. – WhozCraig Jun 02 '15 at 06:17
  • 1
    In the name of all things holy, INDENT YOUR CODE! – Brian Bi Jun 02 '15 at 06:19
  • 1
    Well you can't just pull a `t` out of thin air. If you want to avoid adding `template` before all the member function definitions, then define them inline within the class definition. – Praetorian Jun 02 '15 at 06:33

1 Answers1

0
  1. The template<typename t> is more like property of a class or function than a general declaration. In that sense, yes, the scope is the immediately following function or class.
  2. No, there is no better alternative that I know of. You have to repeate template<typename t>
  3. Just do it like you would with fixed length arrays:

    int length = someFunction();
    double myNumbers[length];
    myNumbers[0] = 3.3;
    
Jan Rüegg
  • 9,587
  • 8
  • 63
  • 105
  • if we have declared it like variable length arrays .. but its size has to be determined at compile time.. – virusai Jun 02 '15 at 06:51
  • does it allocate max possible size for VLA's – virusai Jun 02 '15 at 06:52
  • No, the size does not have to be determined at compile time for VLA's. It allocates the size at runtime (probably on the stack: http://stackoverflow.com/questions/2034712/is-there-any-overhead-for-using-variable-length-arrays) as soon as it knows what the size should be. – Jan Rüegg Jun 02 '15 at 07:21