gpt4 book ai didi

c++ - C++模板方法定义在类中不匹配

转载 作者:行者123 更新时间:2023-12-02 11:05:48 26 4
gpt4 key购买 nike

我无法将模板方法的定义放在其类之外。考虑以下来自.h文件的代码(将非必需的代码剥离以理解该问题):

template <typename T> class CStyleAllocator
{
public:
// Conversion constructor declaration
template <typename U> CStyleAllocator(const CStyleAllocator<U>&);
};

// Attempted definition
template <typename T, typename U> CStyleAllocator<T>::CStyleAllocator(
typename const CStyleAllocator<U>&
)
{
}

Visual C++ 2010编译器输出此错误:
1>c:\dev\devworkspaces\dev.main\platform\cpp\memory\cstyleallocator.h(67): error C2244: 'CStyleAllocator<T>::{ctor}' : unable to match function definition to an existing declaration
1> definition
1> 'CStyleAllocator<T>::CStyleAllocator(const CStyleAllocator<U> &)'
1> existing declarations
1> 'CStyleAllocator<T>::CStyleAllocator(const CStyleAllocator<U> &)'
1> 'CStyleAllocator<T>::CStyleAllocator(void)'

我正在尝试定义一个依赖于2个泛型类型的转换构造函数。

将声明和定义合并到类中的工作原理是:
template <typename T> class CStyleAllocator
{
public:
template <typename U> CStyleAllocator(const CStyleAllocator<U>&) { }
};

您看到我在做什么吗?

最佳答案

像这样尝试:

template <typename T>
template<typename U>
CStyleAllocator<T>::CStyleAllocator(
const CStyleAllocator<U>&
)
{
}

关于c++ - C++模板方法定义在类中不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12877834/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com