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++_Templates - Fatal编程技术网

C++ 模板类中的模板函数

C++ 模板类中的模板函数,c++,templates,C++,Templates,我有以下代码: template <class T> class MyClass { public: template <class U> void foo() { U a; a.invoke(); } }; template <class T> class MyClass { public: template <class

我有以下代码:

template <class T>
class MyClass {
    public:
        template <class U>
        void foo() {
            U a;
            a.invoke();
        }
};
template <class T>
class MyClass {
    public:
        template <class U>
        void foo();
};

template <class T> /* ????? */
void MyClass<T>::foo() {
    U a;
    a.invoke();
}
模板
类MyClass{
公众:
模板
void foo(){
U a;
a、 调用();
}
};
我想要这个表格:

template <class T>
class MyClass {
    public:
        template <class U>
        void foo();
};

template <class T> /* ????? */
void MyClass<T>::foo() {
    U a;
    a.invoke();
}
模板
类MyClass{
公众:
模板
void foo();
};
模板/**/
void MyClass::foo(){
U a;
a、 调用();
}
我怎样才能做到这一点?正确的语法是什么?

写下以下内容:

template <class T>
class MyClass {
    public:
        template <class U>
        void foo();
};

template <class T> /* ????? */
void MyClass<T>::foo() {
    U a;
    a.invoke();
}
template <class T>
template <class U>
void MyClass<T>::foo() { /* ... */ }
模板
模板
void MyClass::foo(){/*…*/}

void MyClass::foo()。。。谢谢,我以前试过,但对我不起作用。。也许我必须做一个干净的项目。@user1074367:不,我想这是我说的。实际上我写了:模板模板void MyClass::foo(){U a;a.invoke();}并且works@user1074367:呃。。。是的,这就是我在回答中所说的,不是吗?@mike:成员模板是非常正常和常见的事情。为什么不在类decl中执行函数decl(参见)?无论如何,您不能将函数decl移出头,因此…@hiobs:FWIW,您可以将声明移动到CPP文件中。也就是说,我只做过一次黑客。在这种情况下,知道如何做到这一点至关重要。有时,在定义函数体所需的依赖项之后,必须将函数定义移到类之外。当类A使用类B,而B也使用A时,就会发生这种情况。在这种情况下,您需要声明A和B,然后定义A和B方法。