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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:21:43+00:00 2026-05-15T03:21:43+00:00

I implemented a Simple STL map in C++. Factored out the comparison as a

  • 0

I implemented a Simple STL map in C++. Factored out the comparison as a type as I was instructed to, then implemented the comparison as shown below:

template <typename T> int KeyCompare<T>::operator () (T tKey1, T tKey2)
{

        if(tKey1 < tKey2)
        return -1;
    else if(tKey1 > tKey2)
        return 1;
    else 
        return 0;
} 

here, tKey1 and tKet2 are the two keys I’m comparing. This worked well for all the basic data types and string. I added a template specialization to compare keys of a user defined type named Test and added a specialization as follows:

int KeyCompare<Test>::operator () (Test tKey1, Test tKey2)
{

        if(tKey1.a < tKey2.a)
        return -1;
    else if(tKey1.a > tKey2.a)
        return 1;
    else 
        return 0;
}

when I run this, I get a linking error saying

SimpleMap.obj : error LNK2005: “public: int __thiscall KeyCompare::operator()(class Test,class Test)” (??R?$KeyCompare@VTest@@@@QAEHVTest@@0@Z) already defined in MapTest.obj

SimpleMap.obj : error LNK2005: “public: __thiscall KeyCompare::~KeyCompare(void)” (??1?$KeyCompare@VTest@@@@QAE@XZ) already defined in MapTest.obj

SimpleMap.obj : error LNK2005: “public: __thiscall KeyCompare::KeyCompare(void)” (??0?$KeyCompare@VTester@@@@QAE@XZ) already defined in MapTest.obj

MapTest.cpp is the test harness class in which I wrote the test case. I have used include guards as well, to stop multiple inclusions.

Any idea what the matter is??

Thank you very much!!

  • 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-15T03:21:44+00:00Added an answer on May 15, 2026 at 3:21 am

    That’s not a specialization.

    Also, please show the whole code. You seem to have templatized the operator () method. This is rather unorthodox. Instead, templatize the whole class and specialize it properly.

    So instead of your code, write something like this:

    template <typename T>
    struct KeyCompare {
        int operator ()(T const& key1, T const& key2) const {
            // Comparison logic here …
        }
    };
    

    And then specialize the class:

    template <>
    struct KeyCompare<Test> {
        int operator()(Test const& key1, Test const& key2) const { … }
    };
    

    This is slightly more code but makes it truly extensible (since anyone can add their own specialization implementation without having to modify existing code). This is also the way that other C++ libraries (in particular the STL) work.

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

Sidebar

Related Questions

I have implemented a simple file upload-download mechanism. When a user clicks a file
In our desktop application, we have implemented a simple search engine using an inverted
I have the simple class using auto-implemented properies: Public Class foo { public foo()
Is there a simple way to retrieve the length of an associative array (implemented
Simple question: I'm researching SMS gateways for the U.S. We've implemented in Europe before,
I have a simple form on a view page, implemented as a user control,
We need to implement a simple state machine in C . Is a standard
I want to implement a simple debug log, which consists of a table into
I've been struggling to get a Prüfer sequence algorithm implemented in Clojure, primarily as
What is the simplest way of testing if an object implements a given interface

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.