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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:32:54+00:00 2026-05-12T05:32:54+00:00

What is the alternative if I need to use a reference, and the data

  • 0

What is the alternative if I need to use a reference, and the data I am passing I cannot change the type of, hence I cannot really store a pointer to it?

Code:

    #include <map>     
    #include<iostream>
    #include<string>     

    using namespace std;

    int main()
    {
       string test;
       pair<string, string> p=pair<string, string>("Foo","Bar");
       map<pair<string, string>, string&> m;
       m[make_pair("aa","bb")]=test;

       return 0;
}

Error:

$ g++ MapPair.cpp
/usr/include/c++/3.2.3/bits/stl_map.h:
In instantiation of
std::map<std::pair<std::string,
std::string>, std::string&,
std::less<std::pair<std::string,
std::string> >,
std::allocator<std::pair<const
std::pair<std::string, std::string>,
std::string&> > >': MapPair.cpp:15:
instantiated from here
/usr/include/c++/3.2.3/bits/stl_map.h:221:
forming reference to reference type
std::string&’ MapPair.cpp: In
function int main()': MapPair.cpp:16:
no match for
std::map, std::string&,
std::less >,
std::allocator,
std::string&> > >& [std::pair]’ operator
/usr/include/c++/3.2.3/bits/stl_pair.h:
At global scope:
/usr/include/c++/3.2.3/bits/stl_pair.h:
In instantiation of std::pair<const
std::pair<std::string, std::string>,
std::string&>':
/usr/include/c++/3.2.3/bits/stl_tree.h:122:
instantiated from
std::_Rb_tree_node

What am I doing wrong to cause this errror?

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

    You cannot store references. References are just aliases to another variable.

    The map needs a copy of the string to store:

    map<pair<string, string>, string> m;
    

    The reason you are getting that particular error is because somewhere in map, it’s going to do an operation on the mapped_type which in your case is string&. One of those operations (like in operator[], for example) will return a reference to the mapped_type:

    mapped_type& operator[](const key_type&)
    

    Which, with your mapped_type, would be:

    string&& operator[](const key_type& _Keyval)
    

    And you cannot have a reference to a reference:

    Standard 8.3.4:

    There shall be no references to references, no arrays of references, and no pointers to references.


    On a side note, I would recommend you use typedef‘s so your code is easier to read:

    int main()
    {
        typedef pair<string, string> StringPair;
        typedef map<StringPair, string> StringPairMap;
    
        string test;
    
        StringPair p("Foo","Bar");
        StringPairMap m;
        m[make_pair("aa","bb")] = test;
    
       return 0;
    

    }

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

Sidebar

Ask A Question

Stats

  • Questions 226k
  • Answers 226k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer A common solution is to define a background color, if… May 13, 2026 at 1:09 am
  • Editorial Team
    Editorial Team added an answer Are you the one adding the item? If so, write… May 13, 2026 at 1:09 am
  • Editorial Team
    Editorial Team added an answer A little known fact is that you can concatenate more… May 13, 2026 at 1:09 am

Related Questions

I need to create a photo gallery for a website running IIS 4.0 or
So, I have a situation where I need to pass in three values into
I'm writing a small article on humanly readable alternatives to Guids/UIDs, for example those
I am currently working on a website that will have a high volume of

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.