关于实现静态成员函数模板时重定义错误的c++问题



我尝试在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_typeB_type可能意味着具有该名称的类型,对于编译器来说,它们只是TU的标识符。这段代码等价于:

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函数成员两次在同一个翻译单元,这是一个错误,即使两个定义是完全相同的

最新更新