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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:09:36+00:00 2026-06-12T19:09:36+00:00

Possible Duplicate: C++ template, linking error I am attempting to implement a selection sort,

  • 0

Possible Duplicate:
C++ template, linking error

I am attempting to implement a selection sort, but I keep getting the error(printed below). It seems to me that all my includes and templates are done correctly. Can someone explain to me the reason for this error and the general approach to debugging this type of error. It usually seems to happen when there is an include or template issues, but occasionally it occurs in situation where I have no idea what is wrong. Thanks.

error LNK2019: unresolved external symbol “public: void __thiscall Selection::SelectionSort(int * const,int)” (?SelectionSort@?$Selection@H@@QAEXQAHH@Z) referenced in function _main

test.cpp

#include <iostream>
#include "SelectionSort.h"
using namespace std;

void main()
{
    int ar[] = {1,2,3,4,5};
    Selection<int> s;
    s.SelectionSort(ar,5);

    for(int i = 0; i < 5; i++)
    {

        cout << "\nstudent number " << i + 1<< " grade " << ar[i];
    }
}

SelcectionSort.h

template<class ItemType>
class Selection
{
public:
    void SelectionSort(ItemType[], int);
private:
    int MinIndex(ItemType[], int, int);
    void Swap(ItemType& , ItemType&);
};

SelectionSort.cpp

#include "SelectionSort.h"

template<class ItemType>
void Selection<ItemType>::SelectionSort(ItemType values[], int numValues)
// Post: The elements in the array values are sorted by key.
{
int endIndex = numValues-1;
for (int current = 0; current < endIndex; current++)
Swap(values[current],
values[MinIndex(values, current, endIndex)]);
}

template<class ItemType>
int Selection<ItemType>::MinIndex(ItemType values[], int startIndex, int endIndex)
// Post: Returns the index of the smallest value in
// values[startIndex]..values[endIndex].
{
int indexOfMin = startIndex;
for (int index = startIndex + 1; index <= endIndex; index++)
if (values[index] < values[indexOfMin])
indexOfMin = index;
return indexOfMin;
}

template<class ItemType>
inline void Selection<ItemType>::Swap(ItemType& item1, ItemType& item2)
// Post: Contents of item1 and item2 have been swapped.
{
ItemType tempItem;
tempItem = item1;
item1 = item2;
item2 = tempItem;
}
  • 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-12T19:09:38+00:00Added an answer on June 12, 2026 at 7:09 pm

    Move the contents of SelectionSort.cpp to SelectionSort.h, just below the class declaration. Also ensure that you have a header guard around the contents of your entire .h file.

    The problem comes from how C++ implements templates. Every time it sees a new type used with a template class (like Selection<int>), it recreates the entire class, replacing ItemType with int.

    For this reason, it needs to know the full definition of the class (along with its methods) at compile time. It cannot just use the class definition and delay linking until later.

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

Sidebar

Related Questions

Possible Duplicate: C++ template, linking error I have two linking errors and I have
Possible Duplicate: trailing return type using decltype with a variadic template function I'm getting
Possible Duplicate: Confusing Template error I have a templated class with a templated method.
Possible Duplicate: make_unique and perfect forwarding Why does C++11 have a make_shared template, but
Possible Duplicate: C++ Inherited template classes don't have access to the base class I'm
Possible Duplicate: change layout and template in zend dynamically according to the device(android tab,iphone,ipad)
Possible Duplicate: How to use enable_if to enable member functions based on template parameter
Possible Duplicate: Where and why do I have to put the “template” and “typename”
Possible Duplicate: Templates: template function not playing well with class’s template member function template
Possible Duplicate: C++ HTML template framework, templatizing library, HTML generator library Planning to write

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.