0

Outer.h :

template<typename T>
class Outer
{
private:

    class Inner
    {
    public:

        int m_int;
        T * m_template;

        Inner(int p_int, const T & p_template);

        ~Inner();
    };
    Inner * m_innerList[];
    void createInner(int value, const T & template);
    Inner * getInner(int p_value) const;

Outer.cpp :

#include "Outer.h"


template<typename T>
void Outer<T>::createInner(int p_value, const T & template) const
{
        Inner * newInner = new Inner(p_value, template);
        m_innerList.add(newInner);
}

template<typename T>
Outer<T>::Inner * Outer<T>::getInner(int p_value) const
{
        for(int i = 0 ; i < nbInner ; i++)
        {
                if(m_innerList[i]->m_int == p_value)
                {
                        return m_innerList[i];
                }
        }
}

The code is not full, but my main problem is that it seems that I cannot create a new instance of the "Inner" class inside the "Outer.cpp" nor can I return an instance of the "Inner" class.

I figured I have some problem with the "Outer::" path, or it might be related to the private inside "Outer.h", but I haven't been able to find a working solution yet.

Thanks for your help!

  • "Outer.cpp" You know that you'll run into trouble when you provide the definition of class template member functions / function templates in a cpp file instead of the header? – dyp Oct 31 '13 at 00:35
  • 1
    "I cannot create a new instance" - what's stopping you, exactly? Do you get a compiler error, and if so, what's the error message? Do you experience some other symptoms, and if so, which ones? – Igor Tandetnik Oct 31 '13 at 00:37
  • `getInner` declaration requires `typename`, as in `template typename Outer::Inner* Outer::getInner(int p_value) const {...}`. The rest seems roughly OK to me, and in fact a simplified version of it [compiles](http://ideone.com/R0BRox) – Igor Tandetnik Oct 31 '13 at 00:48
  • That isn't the only problem. You're going to have another entirely different issue when you try to link code with this thing, since as-written no actual op-code is generated from compiling `Outer.cpp` [Read this for why](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). If you explicitly instantiate somewhere in `Outer.cpp` it will still work, but I don't see you doing that in this code. – WhozCraig Oct 31 '13 at 00:57
  • Thanks @IgorTandetnik! I only needed to add the typename in front of my fonction to make it work! – Gilbert White Nov 03 '13 at 19:15

0 Answers0