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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:17:50+00:00 2026-05-27T12:17:50+00:00

When I tried to compile the following codes in VS2010, error C2678 prompted. #include

  • 0

When I tried to compile the following codes in VS2010, error C2678 prompted.

#include <string>
#include <map>
using namespace std;
class test
{
    private:
        map<string, string> data;
    public:
        test(){};
        ~test(){};
    public:
        const string & get(const string & key)const{return data[key];}; //error C2678
        bool set(const string & key, const string & value){data[key]=value;return true;};
};
void main()
{
    const string key="Hello world!";
    const string value="I'm coming!";
    test t;
    t.set(key,value);
    t.get(key);
}

But when I leave it as functions like

#include <string>
#include <map>
using namespace std;
bool set(const string & key, const string & value, map<string, string> & data)
{
    data[key]=value;
    return true;
}
const string & get(const string & key, map<string, string> & data)
{
    return data[key];
}
void main()
{
    const string key="Hello world!";
    const string value="I'm coming!";
    map<string, string> data;
    set(key, value, data);
    get(key;
}

It does compile and runs.

Does anyone know what’s the problem about?

  • 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-27T12:17:51+00:00Added an answer on May 27, 2026 at 12:17 pm

    You’ve declared the get member function of your test class as const. But the std::map operator[] is a non const function, so it cannot be called from a const function. Use the find function instead.

    The reason operator[] is non const is because if the key does not exist, then it inserts it into the map along with a default constructed value.

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

Sidebar

Related Questions

I tried following code : #include<iostream> #include<string> using namespace std; string f1(string s) {
I tried the following code: #include <iostream> using std::cout; using std::ostream; class X {
I downloaded System.Data.SQLite , and tried to compile the following sample code. using System;
I've got a very strange g++ warning when tried to compile following code: #include
I tried to compile the following code on ubuntu 11.04: #include <boost/interprocess/shared_memory_object.hpp> #include <iostream>
Using DMD 2.057, I cannot get the following code to compile: import std.stdio; import
I tried following the README file in Ruby 1.9.1 but I can't compile it
I tried to compile this example: #include <stdio.h> #include <stdlib.h> #include <stddef.h> main(){ size_t
I tried to compile Ercia Sadun's sample code here , but this error came
I got this error when I tried to compile an application that includes the

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.