4

When I try to run my program this error shows up "error C2955: 'FOURTEEN' : use of class template requires template argument list"

#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;
}

Do I have to retype template somewhere else?

1 Answer 1

5

Members of a template class are themselves templates parameterized by the parameters of their template class. When such a member is defined outside its class, it must explicitly be declared a template.

So you need change

void FOURTEEN::ReadData()

to

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

And do the same thing to function DisplayData.

And there're some other errors:

In function ReadData, change

cin>>a.[i];

to

cin>>a[i];

and In function DisplayData, change

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

to

cout<<a[i]<<'\t';
Sign up to request clarification or add additional context in comments.

2 Comments

Would making ReadData() inline help ?
@sameerkarjatkar You mean define it inside the class's defination? Yes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.