简体   繁体   English

有关C ++模板的问题

[英]Question regarding C++ Templates

I used a simple class for a test program about templates, this is what I did: 我为有关模板的测试程序使用了一个简单的类,这就是我所做的:

template <typename T>
class test
{
public:
    test<T>::test();
    T out();
};

template <typename T>
test<T>::test()
{
}

T test<T>::out()
{
}

int main()
{
    //test<int> t;
}

But when I try to compile it says 'T' : undeclared identifier and use of class template requires template argument list , pointing to the same line, where I have implemented the method out() . 但是,当我尝试编译时,它说“ T”:未声明的标识符类模板的使用需要模板参数列表 ,指向同一行,在该行中我实现了方法out() Can anyone please explain what the problem is?? 谁能解释这个问题是什么? I'm using visual studio 2008. 我正在使用Visual Studio 2008。

Following is more accurate: 以下更准确:

template <typename T>
class test
{
public:
    test();
    T out();
};

template <typename T>
test<T>::test()
{
}

template <typename T>
T test<T>::out()
{
}

1) Don't use <T> inside class 2) Don't forget to declare template <T> before each method declaration out of class body 1)不要在类内部使用<T> 2)不要忘记在类主体之外的每个方法声明之前声明模板<T>

Your definition of the out member is missing the template argument list. 您对out成员的定义缺少模板参数列表。 out should read:- 出来应该是:

template <typename T>
T test<T>::out() 
{ 
} 

This line is wrong: 这行是错误的:

test<T>::test();

Just write this: 只需写下:

test();

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

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