Search code examples
c++templatesinitialization-list

initialize a member of some class in initialization list if the member is a template class


[Solved]: The problem was not in template class initialization, but with code-specific issue of using undefined macro inside a template class constructor. The compiler error did not complain about undefined symbol, but was (wrongfully) related to lambdas.

I've searched for an answer but couldn't find an exact one. The closest answer is here: C++ invoke explicit template constructor but I'm not sure if that is entirely related to my question. And my question is, how can I initialize a member of structure B in initialization list if the member is a template class?

Header ClassA.h:

#ifndef _A_
#define _A_
#include <typeinfo>
#include <windows.h>

template<class Type> class A{
        int u,v;
        Type** pointer;
    public:
        A();
        A(int number);
        ~A();
        Type& operator[] (int i){
            typeid(Type);
            return *pointer[i];
        }
        Type& Get(int i)
        {
            typeid(Type);
                return *pointer[i];
        }
        Type *GetPointer(int i) 
        {
            typeid(Type);
                return pointer[i];
        }   
        Type* add ();
        Type& add(Type *element);
        Type& add(Type *element, int place);
        void expand(int NewLength);
        void swap(Type *element, int place);
        void remove(int number);
        void remove(Type *element);
        void removePointer(int number);
        void removePointer(Type *element);
    };
template<class Type>A<Type>::A(){
    u = 128;
    v = 10;
}
template<class Type>A<Type>::A(int number){
            //some thing to do with number;
            u = number;
            v = 10;
            New( pointer, Type *[u] );
        }  
template <class Type> A<Type>::~A()
{
}
template <class Type> void A<Type>::expand(int NewLength)
{

    Type **NewList = NULL;

    NewList = new Type*[NewLength];
}
template <class Type> Type* A<Type>::add ()
{

    pointer[u] = new Type;
}
template <class Type> Type& A<Type>::add(Type *element)
{
}

template <class Type> Type& A<Type>::add(Type *element, int place)
{
}
template <class Type> void A<Type>::swap(Type *element, int place)
{
}
template <class Type> void A<Type>::remove(Type *element)
{
}
template <class Type> void A<Type>::removePointer(int nume)
{
}
template <class Type> void A<Type>::removePointer(Type *element)
{
}
#endif

Header StructB.h:

#pragma once
#ifndef _B_
#define _B_

#include "ClassA.h"
struct C{
    float x,y,z;


};
struct B{
    private:
        B(){
        }
    public:
        int x,y;
        A<B*> member1;
        A<C> member2;

        B(int X,int Y) : member1(5),member2(5) {
            //initialize x,y
        }

        void Add(B* otherB){

            B** _pOtherB = new B*; (*_pOtherB) = otherB;
            member1.add(_pOtherB);

        }

    };

#endif

The compiler complains with this error (and some other errors, I can post them if nedded):

error C3493: 'number' cannot be implicitly captured because no default capture mode has been specified

Is there any way to do this, or some workaround perhaps?

Thanks in advance :D


Solution

  • Either the code you've given us isn't complete, or it is broken. This line:

    New(pointer, Type *[u]);
    

    seems to be referencing either some missing member method or global function, or it is simply invalid. The error message is kinda cryptic, but that's C++ for you.

    I'm going to assume that New is some kind of macro, because no normal function (even a templated one) can take this sort of type definition as a parameter. You've not given us the definition of New, so there's no way we can tell. It is probably the absense of this macro (maybe a wrapper for some sort of memory debugging system?) that is causing the crazy error.

    If I replace the New line with this:

    pointer = new Type*[u];
    

    the code compiles fine.