简体   繁体   English

如果成员是模板类,则在初始化列表中初始化某个类的成员

[英]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. 编译器错误并未抱怨未定义符号,但(错误地)与lambda有关。

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. 最接近的答案是: C ++调用显式模板构造函数,但是我不确定这是否与我的问题完全相关。 And my question is, how can I initialize a member of structure B in initialization list if the member is a template class? 我的问题是,如果成员是模板类,如何在初始化列表中初始化结构B的成员?

Header ClassA.h: 标头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: 标头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 错误C3493:无法隐式捕获“数字”,因为未指定默认捕获模式

Is there any way to do this, or some workaround perhaps? 有没有办法做到这一点,或者有一些解决方法?

Thanks in advance :D 提前致谢

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. 该错误消息有点含糊,但是对于您来说这是C ++。

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. 我将假设New是某种宏,因为任何普通函数(甚至是模板化的函数)都不能将这种类型定义作为参数。 You've not given us the definition of New , so there's no way we can tell. 您尚未给我们New的定义,所以我们无法告诉您。 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: 如果我用以下内容替换New行:

pointer = new Type*[u];

the code compiles fine. 代码可以正常编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM