Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6152485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:55:42+00:00 2026-05-23T19:55:42+00:00

Technique 1 -> Results in error I have three files MyType.h , MyType.cpp &

  • 0

Technique 1 -> Results in error

I have three files MyType.h, MyType.cpp & main.cpp

MyType.h

#ifndef MYTYPE_H
#define MYTYPE_H

#include<iostream>
using namespace std;

template <class T,int iMax>

class A{
    T iData;
public:
    void vSetData(T iPar1);
    void vDisplayData();
};
#endif

MyType.cpp

#include"MyType.h"

void A::vSetData(T iPar1){
    if(iPar1 <= iMax)
        iData = iPar1;
}
void A::vDisplayData(){
    cout<<"\nData is: "<<iData<<endl;
}

main.cpp

#include"MyType.h"

typedef A<int,20> MyType;

int main(){
    int x = 12;
    MyType obj;
    obj.vSetData(12);
    obj.vDisplayData();
    return 0;
}

ERRORS: 10 errors. They are as follows:-

  • mytype.cpp(2) : error C2955: ‘A’ : use of class template requires template argument list
  • mytype.h(9) : see declaration of ‘A’
  • mytype.cpp(2) : error C2955: ‘A’ : use of class template requires template argument list
  • mytype.h(9) : see declaration of ‘A’
  • mytype.cpp(2) : error C2065: ‘T’ : undeclared identifier
  • mytype.cpp(2) : error C2146: syntax error : missing ‘)’ before identifier ‘iPar1’
  • mytype.cpp(2) : error C2761: ‘void A::vSetData(T)’ : member function redeclaration not allowed
  • mytype.cpp(2) : error C2059: syntax error : ‘)’
  • mytype.cpp(2) : error C2143: syntax error : missing ‘;’ before ‘{‘
  • mytype.cpp(2) : error C2447: ‘{‘ : missing function header (old-style formal list?)
  • mytype.cpp(6) : error C2955: ‘A’ : use of class template requires template argument list
  • mytype.h(9) : see declaration of ‘A’
  • mytype.cpp(6) : error C2509: ‘vDisplayData’ : member function not declared in ‘A’
  • mytype.h(9) : see declaration of ‘A

Technique 2 -> Works fine.

AboveCodeInOneFile.cpp

#include<iostream>
using namespace std;

template <class T,int iMax>

class A{
    T iData;
public:
    void vSetData(T iPar1){
        if(iPar1 <= iMax)
            iData = iPar1;
    }
    void vDisplayData(){
        cout<<"\nData is: "<<iData<<endl;
    }
};

typedef A<int,20> MyType;

int main(){
    int x = 12;
    MyType obj;
    obj.vSetData(12);
    obj.vDisplayData();
    return 0;
}

Please let me know what mistake i am doing in Technique 1

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:55:43+00:00Added an answer on May 23, 2026 at 7:55 pm

    Each method definition needs the full class name decoration above it:

    template <typename T>
    void A<T>::vSetData(T iPar1){
        if(iPar1 <= iMax)
            iData = iPar1;
    }
    
    template <typename T>
    void A<T>::vDisplayData(){
        cout<<"\nData is: "<<iData<<endl;
    }
    

    When you do this however you’ll get other compile errors, for the reasons Abhineet outlined. To fix this, you could put the definitions I wrote into the header file, beneath the class declaration, or you could put it in another header (MyType_Inc.h) and include /that/ header in the class declaration header

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bunch of .cpp files which together process similar data files but
Does anyone have a technique for generating SQL table create (and data insert) commands
What is the proper technique to have ThreadA signal ThreadB of some event, without
I have recently read the safe bool idiom article. I had seen this technique
I am using LINQ to EF and have the following LINQ query: var results
Say I have a page that contains dynamic content (e.g. search results). What is
We have a lot of GNU Make-files. I´d like to time each target used
The technique for adding a reference to the COM interop of Office in Visual
What technique should we use to make the httpsession object not loaded heavily with
Does anybody know a technique to discover memory leaks caused by smart pointers? I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.