-3

Have a custom std::list class helper:

#ifndef __WLIST_H

    #define __WLIST_H

    #include <list>
    using namespace std;

    template <class T >
    class WList : public std::list<T>
    {
        public:
            void wAdd();
            void wRemove();
            bool wContains();
            void wGet();
            void wClear();
            int  wSize();

    };

#endif /*__WLIST_H*/

Howto declare the custom add new item?, in the class declare:

#include "WList.h"

template<class T>
void WList<T>::wAdd(/*???*/)
{
    this->push_back(/*???*/);
}

Howto set the object type in the argument?

e-info128
  • 3,073
  • 8
  • 33
  • 49

2 Answers2

0

Although you should not inherit from stl containers I will give you the answer with your same example.

When using a function or a class template you have to implement the code in the header file, otherwise, the compiler will not instantiate the right functions. When defining function templates the compiler creates a specific class of the function template for the given class. For example, when you do WList<Rectangle> the compiler creates an instance of the WList for the template Rectangle. To do so the compiler needs all the function/class templates in the code that is compiling, otherwise, some functions (the ones that are not in the header file) will be missing and a func is not defined or undefined reference to func error happens.

Your code should be something like:

WList.h

#include <list>

template <typename T >
class WList : public std::list<T>
{
public:
    void wAdd(T obj)
    {
        this->push_back(obj);
    }    
};

or

#include <list>

 template <typename T >
 class WList: public std::list<T>
 {
     public:
         void wAdd(T obj);

 };

 template<typename T>
 void WList<T>::wAdd(T obj)
 {
     this->push_back(obj);
 }

Main

int main() {

    WList<double> list;
    list.wAdd(2);
}

A more proper way of doing this would be without inheriting from the stl container:

#include <list>

template <typename T >
class WList
{
public:
    void wAdd(T obj)
    {
        m_data.push_back(obj);
    }    
protected:
    std::list<T> m_data;
};

Moreover, note that using template<typename T> is better than template<class T> to avoid static ambiguity between class as template and class as class definition

apalomer
  • 1,745
  • 13
  • 28
  • 1
    An even more proper way: `m_data.push_back(std::move(obj));`. – Daniel Langr Jan 22 '18 at 09:02
  • only if @e-info128 uses [c++11 though](http://en.cppreference.com/w/cpp/utility/move), isn't it? – apalomer Jan 22 '18 at 09:07
  • and if declare function name in the header but declare functionality in the class? is `WList::wAdd(T obj)`? – e-info128 Jan 24 '18 at 01:59
  • You can declare the function outside of the class definition, but if the function is not declared in the header file then it won't be instantiated when the class for a given type is instantiated. Look at my edit. – apalomer Jan 24 '18 at 08:18
0

In C++11, if it's available:

Templates need to be defined in a header file, you can write inside a class:

void wAdd(T&& obj) { this->push_back(std::move(obj)); }   
void wAdd(const T& obj) { this->push_back(obj); }

You might also want to consider emplace semantics:

template <typename... Ts>
void wEmplace(Ts&&... args) { this->emplace_back(std::forward<Ts>(args)...); }

And do not use using namespace std; in a header file!

Daniel Langr
  • 20,369
  • 3
  • 44
  • 81