【问题标题】:Error: Use of class template requires template argument list错误:使用类模板需要模板参数列表
【发布时间】:2014-04-22 06:16:45
【问题描述】:

当我尝试运行我的程序时,此错误显示“错误 C2955:'FOURTEEN':使用类模板需要模板参数列表”

#include <iostream>
using namespace std;
template <class T, int n>
class FOURTEEN
{
private:
    T a[n];
public:
    void ReadData();
    void DisplayData();
};
void FOURTEEN::ReadData()
{
    for(int i=0;i<n;++i)
        cin>>a.[i];
}
void FOURTEEN::DisplayData()
{
    for(int i=0;i<n;++i)
        cin>>a.[i]<<'\t';
    cout<<endl;
}
int main()
{
    FOURTEEN <int, 5>P;
//Read data into array a of object P
    cout<<"Enter 5 numbers: ";
    P.ReadData();
//display data of array a of object P
    P.DisplayData();

    system("pause");
    return 0;
}

我必须在其他地方重新输入模板吗?

【问题讨论】:

    标签: c++ class templates


    【解决方案1】:

    模板类的成员本身就是由参数参数化的模板 他们的模板类。当这样的成员在其类之外定义时,它必须显式地 声明了一个模板。

    所以你需要改变

    void FOURTEEN::ReadData()
    

    template <class T, int n>
    void FOURTEEN<T, n>::ReadData()
    

    DisplayData 执行同样的操作。

    还有一些其他错误:

    在函数ReadData中,改变

    cin>>a.[i];
    

    cin>>a[i];
    

    在函数DisplayData中,改变

    cin>>a.[i]<<'\t';
    

    cout<<a[i]<<'\t';
    

    【讨论】:

    • 内联 ReadData() 会有帮助吗?
    • @sameerkarjatkar 你的意思是在类的定义中定义它?是的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多