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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:12:55+00:00 2026-06-15T13:12:55+00:00

A puzzling question to C++ template and type conversion is shown below… To make

  • 0

A puzzling question to C++ template and type conversion is shown below…
To make my life easier, I defined a template class to model a one-to-one relationship using a class BiMap:

#include <stdio.h>
#include <stdlib.h>
#include <map>   
#include <string>

template<class T1, class T2>
class BiMap {
 public:
  T2& operator[] (T1& t1) {
    return d1[t1];
  }
  T1& operator[] (T2& t2) {
    return d2[t2];
  }
 private:
  std::map<T1, T2> d1;
  std::map<T2, T1> d2;
};


int main(int argc, char *argv[])
{

  BiMap<std::string, int> m;
  m["1"] = 2;
  m[2] = 3;

  printf("%d", m["1"]);
  printf("%d", m[2]); 
  return 0;     
}

But I get this compiling error:

testPedigree.cpp:45: error: no match for ‘operator[]’ in ‘m["1"]’
testPedigree.cpp:16: note: candidates are: T2& BiMap<T1, T2>::operator[](T1&) [with T1 = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, T2 = int]
testPedigree.cpp:19: note:                 T1& BiMap<T1, T2>::operator[](T2&) [with T1 = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, T2 = int]

I was expecting C++ will automatically cast const char* to std::string, as I asked the question in this post: Why I can use const char* as key in std::map<std::string, int>

  • 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-15T13:12:56+00:00Added an answer on June 15, 2026 at 1:12 pm

    Your code tries to bind a non-const reference to a temporary (namely, the std::string which is implicitly created from your const char[]). Only const references can be bound to temporaries.

    Try:

      T2& operator[] (const T1& t1) {
        return d1[t1];
      }
      T1& operator[] (const T2& t2) {
        return d2[t2];
      }
    

    You have some other trivial bugs in your sample. Here is a corrected, tested program:

    #include <stdio.h>
    #include <stdlib.h>
    #include <map>
    #include <string>
    
    template<class T1, class T2>
    class BiMap {
     public:
      T2& operator[] (const T1& t1) {
        return d1[t1];
      }
      T1& operator[] (const T2& t2) {
        return d2[t2];
      }
     private:
      std::map<T1, T2> d1;
      std::map<T2, T1> d2;
    };
    
    
    int main(int argc, char *argv[])
    {
    
      BiMap<std::string, int> m;
      m["1"] = 2;
      m[2] = "3";
    
      printf("%d", m["1"]);
      printf("%s\n", m[2].c_str());
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This one has been puzzling my for some time now. Let's imagine a class
I'm a new Ruby/Rails guy. Here's one question puzzling me: Can we find the
Ok this one is puzzling me. I have an abstract base class named Testbase
This question has been puzzling me for a long time now. I come from
I have a big question that has been puzzling me for a long time
This one's puzzling me. I have a MySQL query, being run though PDO: $stmt
I have a puzzling issue regarding modules defined in the lib dir I have
this one been puzzling me for a couple of searching hours. So I have
I have a model which uses acts-as-tree. For example: class CartoonCharacter < ActiveRecord::Base acts_as_tree
I have another very puzzling question that has cropped up after my upgrade from

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.