2

I have an assignment at my university. My task is to complete two header files with the implementation of the given classes. It is important to implement in those header files, because it will be tested by unit tests and only the header files will be tested.

Let's say I have this .h file:

#pragma once

template<class T>
class RareVector;

template<class T>
class Vector
{
public:
Vector(){}
Vector(int dim);
Vector(T *t, int dim);
Vector(const Vector&);
~ Vector();

Vector operator+(const Vector&);
Vector operator-(const Vector&);
double operator*(const Vector&);
double operator~();
double operator%(const Vector&);
T      operator[](int) const;
operator RareVector<T>();

private:
T*  m_t;
int m_dim;
};

My question is: the only place to implement these classes is at the declaration, or I can do something like this somewhere below:

template <class T>
Vector<T>::Vector(){
// code goes here
}
3
  • 1
    follow your own example at line 10 Commented May 24, 2017 at 10:27
  • So, this is the only way, isn't it? Commented May 24, 2017 at 10:37
  • this is the only place where it would make sense, if you define it later like in your second block of code then you're defining something that was previously declared but not defined, this basically should be in a separate source file that is not included anywhere else, if you put it in the header the compiler will complain if this file is included by multiple source files because it will see multiple definitions, wherein if you define it within the class it has a way to mitigate that Commented May 24, 2017 at 10:53

1 Answer 1

1

You can use inline to implement functions outside of the declaration...

template<class T>
class Vector
{
public:
Vector(){}
Vector(int dim);
inline Vector(T *t, int dim);
Vector(const Vector&);
~ Vector();

Vector operator+(const Vector&);
...
};


template<class T>
inline Vector::Vector(T *t, int dim)
{
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.