-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,727
  • 10
  • 40
  • 57
  • 2
    Can't you just pass a class of type `T` to `wAdd` (`void WList::wAdd(T object_to_add)`) and then add it like `this->push_back(object_to_add)`? – apalomer Jan 21 '18 at 21:54
  • If you don't know the answer to *Howto declare the custom add new item?*, you need to understand the fundamentals of the language from [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Also, don't derive from `std::list`. The standard prohibits it. – R Sahu Jan 21 '18 at 21:56
  • @apalomer does not work, the compiler says an error: `WList::wAdd(Rectangle) is not defined` – e-info128 Jan 21 '18 at 22:29
  • @RSahu a good book in spanish? – e-info128 Jan 21 '18 at 22:30
  • Spanish? That's a link to [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – R Sahu Jan 21 '18 at 22:33
  • @e-info128 you have to implement it in the header otherwise it won't be instantiated. – apalomer Jan 21 '18 at 22:38
  • Never ever write `using namespace std;` in a header file. – Daniel Langr Jan 22 '18 at 08:57
  • `void wAdd();` add *what*? – n. m. could be an AI Jan 22 '18 at 09:10

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,895
  • 14
  • 36
  • 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
  • 22,196
  • 3
  • 50
  • 93