Category : generics

I split a generic class into a header and implementation file as follows. Test.h: #ifndef MY_DESCRIPTIVE_GUARD_NAME #define MY_DESCRIPTIVE_GUARD_NAME template<typename T> class MyClass { T myObj; public: MyClass(T& obj); void set(T& obj); T get(); } #include "Test.tpp" #endif Test.tpp: template<typename T> MyClass<T>::MyClass(T& obj) { // implementation } // etc. But as I add more methods to ..

Read more

Disclaimer: I’m very new to C++. I have a Container class and an Item class. Both of them are generic with the same Type: container.h: template<typename Type> class Container { public: Container(Item<Type> item); } container.cpp: #include "container.h" #include "item.h" template<typename Type> Container<Type>::Container(Item<Type> item) { } The problem is that an error is showing on the ..

Read more

I have the following templated merge sort program: #include <iostream> #include <vector> #include <string> // trying to create a default method call class CInstance { private: std::string str_; public: CInstance(const std::string& str) : str_(str) {} bool const operator>(const CInstance& that){ return (this->str_.size() > that.str_.size());} }; template<class T> class CObj { private: T val; public: CObj(const ..

Read more

I am trying to restrict the scope of my non-type template parameter as much as possible, so this is the parent template: template <typename _Tp, const std::vector<std::size_t> &_dims> struct __array_traits{}; and I’m trying to create a specialised template with the non-type template parameter set to a default value, possibly an empty const vector: template<typename _Tp> ..

Read more