简体   繁体   English

C ++模板,函数在类中定义?

[英]C++ Template, Functions define in a class?

Class.h

template<class T>
  struct AStruct
  {
     //stuff
  };

class aClass
{
  template<class T>
  void setStruct(const AStruct<T>& s)
  {
    theStruct = s; 
  }

private:
  template<class T>
  AStruct<T> theStruct;  //this is not liked.  "multiple template parameter lists are not allowed" are one of the errors I get... 
}; 

Quite the mess but still, I'd like to know what can be done about it... I've tried playing around a bit but with no luck... Also, only standard library please! 乱七八糟,但我还是想知道该怎么办...我尝试了一下,但是没有运气...而且,请只使用标准库!

template<class T>
struct AStruct{
};

template<class T>         //<-- the template must be here
class aClass {
public:
    void setStruct(const AStruct<T>& s){
         theStruct = s; 
    }
private:
    AStruct<T> theStruct; //<-- To construct this variable
};

Remove the template<class T> 删除template<class T>

template<class T>
struct AStruct{
 //stuff
};

template<class T>
class aClass{

 void setStruct(const AStruct<T>& s){
  theStruct = s; 
}

private:
AStruct<T> theStruct;   
}; 

You already defined AStruct to be a template 您已经将AStruct定义为模板

If you are holding a member that is a class template, then you either need a class template, or you need to provide a type for the template parameter. 如果您持有的成员是类模板,那么您要么需要一个类模板,要么需要为template参数提供类型。 It seems like you need the former: 似乎您需要前者:

template<class T>
class aClass
{
  void setStruct(const AStruct<T>& s){
   theStruct = s; 
  }

private:
  AStruct<T> theStruct; // data member is class template
}; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM