-1

If I have

template<class T> class Vector

public:
   Vector(const Vector& bla);

How can I use it outside the .h file?

I´ve tried Vector<T>::Vector but that doesn´t work.

some part of the .h file

           Vector();

    Vector(int size, T value = T());

    Vector(const Vector& vec);

    ~Vector();

    T at(int index) const;

    void set_value_at(int index, T elem) const;

); //Here is the code in same .h file but outside the class

   Vector();

    Vector(int size, T value = T())
            {}

    Vector(const Vector& vec){}

    ~Vector(){}

    T at(int index) const{}

    void set_value_at(int index, T elem) const{}
  • 1
    Please be more clear. Show the entire `.h` file, as well as where/how you're trying to use it. – AndyG Mar 25 '14 at 22:18

2 Answers2

1

If you are trying to implement Vector(), you do it like this:

template<class T> Vector<T>::Vector(const Vector<T>& bla)
{
    ...
}

If you are trying to use Vector(), you do it like this:

Vector<SomeTypeHere> vec1;
Vector<SomeTypeHere> vec2(vec1);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • If I use it on this function: templateVector::Vector(int size, T value = T()) I get this error: error: default argument given for parameter 2 of'Vector::Vector(int, T)' – user3399655 Mar 25 '14 at 22:36
  • Default parameter values can only be specified in the **declaration**, not in the **implementation**, eg: `template Vector { public: Vector(int size, T value = T()); };` `template Vector::Vector(int size, T value) {...}`, unless they are inlined together (which is most common when using templates), eg: `template Vector { public : Vector(int size, T value = T()) { ... } };` – Remy Lebeau Mar 25 '14 at 22:41
0

If you did everything right, Vector<type> v(args) should automatically call what looks like the constructor. To use other functions:

v.myFunc();

Static functions:

Vector<type>::mystaticfunc();
yizzlez
  • 8,757
  • 4
  • 29
  • 44