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
Do templates have scope or scope like thing
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 alternativeThis 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)
wheren
is known at runtime.