1

[已解决]:问题不在模板类初始化中,而是在模板类构造函数中使用未定义宏的代码特定问题。编译器错误并没有抱怨未定义的符号,而是(错误地)与 lambdas 相关。

我已经搜索了答案,但找不到确切的答案。最接近的答案在这里:C++ 调用显式模板构造函数,但我不确定这是否与我的问题完全相关。我的问题是,如果成员是模板类,我如何在初始化列表中初始化结构 B 的成员?

标题 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

标头结构 B.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

编译器抱怨此错误(以及其他一些错误,如果需要,我可以发布它们):

错误 C3493:无法隐式捕获“数字”,因为未指定默认捕获模式

有没有办法做到这一点,或者一些解决方法?

提前致谢

4

1 回答 1

2

您提供给我们的代码要么不完整,要么已损坏。这一行:

New(pointer, Type *[u]);

似乎引用了一些缺少的成员方法或全局函数,或者它根本无效。错误消息有点神秘,但这对你来说是 C++。

我将假设这New是某种宏,因为没有普通函数(即使是模板函数)可以将这种类型定义作为参数。你没有给我们 的定义New,所以我们无法判断。可能是这个宏的缺失(可能是某种内存调试系统的包装器?)导致了这个疯狂的错误。

如果我New用这个替换该行:

pointer = new Type*[u];

代码编译得很好。

于 2014-05-18T10:30:32.793 回答