Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在C+中实现CRTP链表混合+;_C++_Templates_Mixins_Crtp - Fatal编程技术网

C++ 在C+中实现CRTP链表混合+;

C++ 在C+中实现CRTP链表混合+;,c++,templates,mixins,crtp,C++,Templates,Mixins,Crtp,我很难让CRTP混音器工作 以下是精简的实现: template < typename T > class AutoSList : public T { public: AutoSList() {} ~AutoSList() : _next(nullptr) {} private: static T* _head; static T* _tail; T* _next; }; // I really hate this syntax. template &

我很难让CRTP混音器工作

以下是精简的实现:

template < typename T >
class AutoSList : public T {
public:
  AutoSList() {}

  ~AutoSList() : _next(nullptr) {}


private:
  static T* _head;
  static T* _tail;
  T* _next;

};

// I really hate this syntax.
template <typename T>
T*  AutoSList<T>::_head = nullptr;
template <typename T>
T*  AutoSList<T>::_tail = nullptr;

class itsybase : public AutoSList < itsybase >
{

};
模板
类别列表:公共T{
公众:
自动列表(){}
~AutoSList():_next(nullptr){}
私人:
静态T*_头;
静态T*_尾;
下一步;
};
//我真的很讨厌这种语法。
样板
T*AutoSList::_head=nullptr;
样板
T*AutoSList::_tail=nullptr;
将itsybase分类:公共自动列表
{
};
我正在使用VS2013,并得到以下错误:

   error C2504: 'itsybase' : base class undefined
 : see reference to class template instantiation 'AutoSList<itsybase>' being compiled
错误C2504:'itsybase':基类未定义
:请参阅对正在编译的类模板实例化“AutoSList”的引用

我不知道出了什么问题,有什么建议吗?

这是打字错误,正如
user207933
所建议的那样。下面是关于它的
clang++
说明:

$ clang++ -std=c++11 -c go.cpp 
go.cpp:6:5: error: missing return type for function 'AutoList'; did you mean the constructor name 'AutoSList'?
    AutoList() {}
    ^~~~~~~~
    AutoSList
go.cpp:8:6: error: expected the class name after '~' to name a destructor
    ~AutoList() {}
     ^~~~~~~~
     AutoSList
go.cpp:20:19: error: no member named '_head' in 'AutoSList<T>'
T*  AutoSList<T>::_head = nullptr;
    ~~~~~~~~~~~~~~^
go.cpp:24:25: error: use of undeclared identifier 'ADLib'
class itsybase : public ADLib::AutoSList < itsybase >
                        ^
4 errors generated.
$clang++-std=c++11-c go.cpp
go.cpp:6:5:错误:函数“AutoList”缺少返回类型;你是说构造函数名“AutoSList”吗?
自动列表(){}
^~~~~~~~
自动列表
go.cpp:8:6:错误:应在“~”之后使用类名来命名析构函数
~AutoList(){}
^~~~~~~~
自动列表
go.cpp:20:19:错误:“自动列表”中没有名为“\u head”的成员
T*AutoSList::_head=nullptr;
~~~~~~~~~~~~~~^
go.cpp:24:25:错误:使用未声明的标识符“ADLib”
类itsybase:public ADLib::AutoSList
^
产生了4个错误。

有两个问题导致了这些编译错误。第一个是打字错误,导致c-tor/d-tor和类名不匹配


第二个问题是您试图在父模板中继承
T
。这在CRTP中是不可能的,因为在实例化模板时类型将不完整。这将导致无限递归继承:
itsybase
继承
AutoSList
它继承
itsybase
它继承
AutoSList

这是一个打字错误。您的类名是AutoList,但您用AutoList的名称声明了一个构造函数。析构函数也是一样。@AllanDeutsch:在答案发布后,请不要更改代码或问题的其他部分。这很容易使答案无效。相反,如果必须,请添加到问题中,但如果这些信息使问题变得非常不同,请提出一个新问题。谢谢,我已更新了它以反映这些更改,但仍有一些错误。@AllanDeutsch请在您的问题中明确列出更新,否则您将使答案无效。如果更新提示出现全新问题,请打开新问题。我看了这篇和其他类似的文章,它似乎也在做同样的事情。编辑:没关系,我是个白痴。