关于实现静态成员函数模板时重定义错误的c++问题
C++ questions regards to redefinition error when implement a static member function template?
我尝试在cpp源文件中实现一个静态函数。获取重定义错误。有人能帮我一下吗?非常感谢!
DEFINE_MY_STATIC_PTR(B_type)
DEFINE_MY_STATIC_PTR(A_type)
——error: redefinition
struct A_type : B_type
{}
#define DEFINE_MY_STATIC_PTR( TYPE )
template< typename TYPE >
My_self_deleting* My_static_ptr<TYPE>::my_self_deleting_ptr( TYPE* ptr )
{ return ptr; }
template<typename TYPE>
struct My_static_ptr : My_pointer
{
static My_self_deleting* my_self_deleting_ptr ( TYPE* );
}
如何正确地进行这种定义?
从所有的帮助中,我发现它应该删除上面的模板。当使用时,它将成为每个TYPE的实现。#define DEFINE_MY_STATIC_PTR( TYPE )
My_self_deleting* My_static_ptr<TYPE>::my_self_deleting_ptr( TYPE* ptr )
{ return ptr; }
DEFINE_MY_STATIC_PTR(B_type)
error: too few template-parameter-lists
我应该在这个宏定义中添加更多的东西吗?
是,添加模板<>
#define DEFINE_MY_STATIC_PTR( TYPE )
template <>
My_self_deleting* My_static_ptr<TYPE>::my_self_deleting_ptr( TYPE* ptr )
{ return ptr; }
我不完全确定你想做什么,但让我做一些一般性的评论:
你没有一个类,你有一个类模板。这意味着,通常不能将实现与定义分开,因为定义必须在实例化模板的每个点上都是可见的。因此,典型的解决方案是将所有内容放在header中:// header.hpp
template<typename T>
struct My_static_ptr : My_pointer
{
static My_self_deleting * my_self_deleting_ptr(T * p)
{
return p;
}
};
只有在某些特殊情况下,您才可以考虑提供一组有界的显式模板实例化:
// header.hpp
template<typename T>
struct My_static_ptr : My_pointer
{
static My_self_deleting * my_self_deleting_ptr(T * p);
};
// implementation.cpp
template<typename T> My_self_deleting * My_static_ptr<T>::my_self_deleting_ptr(T * p)
{
return p;
}
// Only the following specializations are usable in your entire program!
template struct My_static_ptr<int>;
template struct My_static_ptr<double>;
template struct My_static_ptr<Foo>;
基本上,模板机制可以用来避免为不同的类型重写相同的算法。
添加宏来声明多个模板引入了模板的重定义。
在你的例子中,模板方法的唯一声明就足够了。你的代码应该是这样的:
// declaration of struct A_type
struct A_type : B_type
{}
// declaration of template class My_static_ptr, of template parameter TYPE
template<typename TYPE>
struct My_static_ptr : My_pointer
{
// declaration of template method my_self_deleting_ptr, of template parameter TYPE
static My_self_deleting* my_self_deleting_ptr ( TYPE* );
}
// definition of template static method my_self_deleting_ptr, of template parameter TYPE
template< typename TYPE >
My_self_deleting* My_static_ptr<TYPE>::my_self_deleting_ptr( TYPE* ptr )
{ return ptr; }
然后你需要用一个具体类型实例化你的模板,这样你就可以使用它了:
// instantiation of the template for A_type, by creating an object My_static_ptr parameterized with A_type
My_static_ptr< A_type > myStaticPointer_of_A_type;
当编译器看到(在本例中是预处理器)时,详细说明为什么会出现特定的错误:
DEFINE_MY_STATIC_PTR(B_type)
DEFINE_MY_STATIC_PTR(A_type)
它应用宏并执行宏参数的文本替换,以:
结尾template< typename B_type >
My_self_deleting* My_static_ptr<B_type>::my_self_deleting_ptr( B_type* ptr )
{ return ptr; }
template< typename A_type >
My_self_deleting* My_static_ptr<A_type>::my_self_deleting_ptr( A_type* ptr )
{ return ptr; }
对于您来说,A_type
和B_type
可能意味着具有该名称的类型,对于编译器来说,它们只是T
或U
的标识符。这段代码等价于:
template< typename T >
My_self_deleting* My_static_ptr<T>::my_self_deleting_ptr( T* ptr )
{ return ptr; }
template< typename T >
My_self_deleting* My_static_ptr<T>::my_self_deleting_ptr( T* ptr )
{ return ptr; }
你有你的多个定义:你已经定义了类模板的my_self_deleting_ptr
函数成员两次在同一个翻译单元,这是一个错误,即使两个定义是完全相同的
相关文章:
- 使用命名空间时出现多个定义错误
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 尝试调用 .h 文件中定义的变量时出现变量未定义错误
- 在头文件和 cpp 文件中使用一次 #pragma 时出现结构重定义错误
- 链接阶段出现多重定义错误
- 避免模板类中的重定义错误
- 即使我没有包含多个文件,C++中的多个定义错误
- 跨多个类的全局变量而不会出现重定义错误?
- 尝试运行 wasm 函数时出现模块未定义错误
- C++ 预期的左大括号以及重定义错误
- C++:成员的越界声明必须是纯虚函数的定义错误
- c++中数组的未定义错误
- Visual C:模板类中的自定义错误消息
- 为什么C++模板不会导致多个定义错误?
- 只有一个定义/声明时标头声明变量的多堆定义错误
- C++ 在多个其他类中使用单个类 - 编译时出现多个定义错误
- 基类未定义.错误 C2504
- Q 斯坦达项重定义错误
- 可视化C++中的结构定义错误
- VC++ C2011 重定义错误 - 未使用的头文件