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 9278531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T17:23:57+00:00 2026-06-18T17:23:57+00:00

I have written a template class, for a map/dictionary data structure, and keep getting

  • 0

I have written a template class, for a map/dictionary data structure, and keep getting this strange error (ERROR LNK2019:unresolved external symbol)

Code:

AssArray.h:

#pragma once   
template <class K,class D>   
class AssArray   
{   
    int _size;   
    int _position;   
    D* _data;   
    K* _key;  

public:    
     AssArray(int);
    ~AssArray(void);

    const D& operator [](K k) const;
    D& operator [](K k);
};

AssArray.cpp:

#include "StdAfx.h"
#include "AssArray.h"

template <class K,class D>
AssArray<K,D>::AssArray(int size)
{
    _size=size;
    _data = new D[size];
    _key = new K[size];
    _position=0;
}

template <class K,class D>
AssArray<K,D>::~AssArray(void)
{
        delete[] _data;
    delete[] _key;
}

template <class K,class D>
const D& AssArray<K,D>::operator [](K k) const
{
    //Get
    for(int i=0;i<_position;i++)
        if(_key[i]==d)
            return _data[i];
    return NULL;
}

template <class K,class D>
D& AssArray<K,D>::operator [](K k)
{
    //Set
    for(int i=0;i<_position;i++)
        if(_key[i]==d)
            return _data[i];

    if(_position<_size-1)
    {
        _key[position]=d;
        _position++;
        return _data[_position];
    }
    else
    {
        //Implement error handling
    }

}

The errors are:
1

"Error  4   error LNK1120: 3 unresolved externals   
C:\Users\*****\Documents\Visual Studio 2010\Projects\OOPLAB4NYARE\Debug
\OOPLAB4NYARE.exe   1   1   OOPLAB4NYARE"

2

Error   1   error LNK2019: unresolved external symbol "public: __thiscall 
AssArray<char *,float>::~AssArray<char *,float>(void)" (??1?$AssArray@PADM@@QAE@XZ)
referenced in function _wmain   C:\Users\*****\Documents\Visual Studio
2010\Projects\OOPLAB4NYARE\OOPLAB4NYARE\OOPLAB4NYARE.obj    OOPLAB4NYARE

3

Error   3   error LNK2019: unresolved external symbol "public: __thiscall
AssArray<char *,float>::AssArray<char *,float>(int)" (??0?$AssArray@PADM@@QAE@H@Z) 
referenced in function _wmain   C:\Users\*****\Documents\Visual Studio 
2010\Projects\OOPLAB4NYARE\OOPLAB4NYARE\OOPLAB4NYARE.obj    OOPLAB4NYARE

4

Error   2   error LNK2019: unresolved external symbol "public: float & 
__thiscall AssArray<char *,float>::operator[](char *)" 
(??A?$AssArray@PADM@@QAEAAMPAD@Z) referenced in function _wmain C:\Users\Jonathan
\Documents\Visual Studio 2010\Projects\OOPLAB4NYARE\OOPLAB4NYARE\OOPLAB4NYARE.obj   
OOPLAB4NYARE

I am using Microsoft visual studio 2010 Ultimate. It’s most likely something easy I’ve just been overlooking.

I have tried to clean->rebuild, created a new project and copy pasted the relevant code, as well as trying to find a solution, but the ones I’ve seen are pretty varied and vague.

  • 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-06-18T17:23:59+00:00Added an answer on June 18, 2026 at 5:23 pm

    You cannot put member function definitions for a class template in .cpp files.

    The definitions of member functions of a class template must be seen by the compiler at the point those functions are invoked, while processing the translation unit (i.e. .cpp file) that contains that invocation. This allows the compiler to actually generate the code. No instantiation occurs unless those functions are invoked.

    Now by putting your function definitions in a .cpp file which contains no invocation of those functions, you are basically:

    1. sealing them into a place where no other .cpp file has a chance to see them;
    2. avoiding any instantiation of those functions in the only .cpp file that can see their definitions.

    Therefore, the compiler will not generate any object code for them, and the linker will eventually complain that the corresponding symbols are not found (that’s the error you get).

    To solve the problem, move the member function definitions of your class template into the same header where the class template definition is (AssArray.h in your case), or in a header which is #included by the translation unit(s) where those functions are invoked (and before the points of invocation).

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

Sidebar

Related Questions

I have written a template function like this: template <class T> const T& max1(const
have written this little class, which generates a UUID every time an object of
I have written the following code: #include <iostream> using namespace std; template <class T>
I have written a list-like template class sll (Single Linked List). Now, I am
I have written a template class for a circular buffer: template <class T> class
I am new to STL. I have written a Template Base class as follows
I have written a function template for serialization of enums to/from our stream class
I have written this OSGI bundle: /* * To change this template, choose Tools
I have written the following wrapper for std::bind and std::queue : #include Queue.h template<class
I have written this class. This is not complete description of original class to

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.