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

  • SEARCH
  • Home
  • 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 6880713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:00:34+00:00 2026-05-27T05:00:34+00:00

I wrote a template class which extends STL list for some use. I declare

  • 0

I wrote a template class which extends STL list for some use. I declare this class in a header file and define it in a cpp file. However, when I tried to test it in another cpp file, I found Visual Studio not able to link the functions to the cpp file.

I know I can solve this problem by just including the cpp file, but I wonder if there is another way, a better way?

In case you require further information of my code, I give my template class header file “KA_Buffer.h” and main program “main.cpp” below.

KA_Buffer.h:

#pragma once

#include <list>

typedef int KA_Time;
typedef std::list<KA_Time> KA_TimeList;

/**
 * This is a length-limited buffer which would only save limited number of
 * elements.
 */
template <class T>
class KA_Buffer :
    public std::list<T>
{
public:
    KA_Buffer(int length) :
      m_MaxLength(length), m_Length(0) {};
    ~KA_Buffer(void) {};

    void add(T);
    void pop_front();

    iterator find(T);

private:
    int m_Length;
    int m_MaxLength;
};

main.cpp:

#include <iostream>
#include <Windows.h>
#include "KA_Buffer.h"

int main()
{
    KA_Buffer<int> buffer(5);
    for (int i = 0; i < 10; i++)
    {
        buffer.add(i);
        for (KA_Buffer<int>::iterator iter = buffer.begin();
            iter != buffer.end(); iter++)
        {
            std::cout<<*iter<<" ";
        }
        std::cout<<std::endl;
    }
    system("pause");
    return 0;
}

The error messages:

1>main.obj : error LNK2019: unresolved external symbol “public: void __thiscall KA_Buffer::add(int)” (?add@?$KA_Buffer@H@@QAEXH@Z) referenced in function _main

  • 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-27T05:00:35+00:00Added an answer on May 27, 2026 at 5:00 am

    Template method definitions need to be present in the header file.

    template <class T>
    class KA_Buffer :
        public std::list<T>
    {
    public:
        KA_Buffer(int length) :
          m_MaxLength(length), m_Length(0) {};
        ~KA_Buffer(void) {};
    
        void add(T) //     <--- add method definition here
        {
        }
        void pop_front() //<--- add method definition here
        {
        }
    
        iterator find(T) //<--- add method definition here
        {
        }
    
    private:
        int m_Length;
        int m_MaxLength;
    };
    

    They don’t have to be present in the function declaration, you can also define the methods outside, as long as you keep them visible to the translation unit using the template:

    template <class T>
    class KA_Buffer :
        public std::list<T>
    {
    public:
        KA_Buffer(int length) :
          m_MaxLength(length), m_Length(0) {};
        ~KA_Buffer(void) {};
    
        void add(T);
        void pop_front();
        iterator find(T);
    
    private:
        int m_Length;
        int m_MaxLength;
    };
    
    // Definitions follow in the header file:
    
    template <class T>
    void KA_Buffer::add(T) 
    {
    }
    template <class T>
    void KA_Buffer::pop_front() 
    {
    }
    template <class T>
    iterator KA_Buffer::find(T) 
    {
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a template class which has a static pointer-to-member, like this: template<class T,
I wrote a template class which is giving compilation error template<class T> class Entity
How can declare template of a template class?? see below code: File: A.h class
For some reasons, I'd like to write code like this: template<class T> class C
I wrote the following program #include <iostream> template<typename C, typename Res, typename... Args> class
I wrote some C++ code in which I used Templates. Since I used templates,
First some background. I'm parsing a simple file format, and wish to re-use the
I wrote a method which exports values to excel file from an IEnumerable parameter.
This is a concurrent queue I wrote which I plan on using in a
I recently wrote a function template which takes a reference to a C-array: template

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.