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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:35:00+00:00 2026-05-23T11:35:00+00:00

Just a little warning: I’ve only been doing C++ for 2 weeks now, expect

  • 0

Just a little warning: I’ve only been doing C++ for 2 weeks now, expect to see stupid beginner errors.

I was writing some (useless) code to get familiar with classes in C++ (it’s a wrapper around a string), and I added a copy constructor, but I keep on getting this error:

pelsen@remus:~/Dropbox/Code/C++/class-exploration> make val
g++ -o val.o val.cpp
val.cpp: In copy constructor ‘CValue::CValue(const CValue&)’:
val.cpp:27: error: passing ‘const CValue’ as ‘this’ argument of ‘const std::string CValue::getData()’ discards qualifiers
make: *** [val] Error 1

I have done research, apparently this error is caused by the copy constructor doing non-const operations. I get that much. In response, I made CValue::getData() a const member. Apart from accessing getData(), the copy constructor doesn’t do anything, so I don’t see why I am still getting the error. Here is (some of) the buggy code:

  7 class CValue {
  8   string *value;
  9 public:
 10   CValue();
 11   CValue(string);
 12   CValue(const CValue& other);
 13   ~CValue();
 14   void setData(string);
 15   const string getData();
 16 };
 17 
 22 CValue::CValue(string data) {
 23   value = new string(data);
 24 }
 25 
 26 CValue::CValue(const CValue& other) {
 27   value = new string(other.getData());
 28 }
 37 
 38 const string CValue::getData() {
 39   return(*value);
 40 }

Does anyone know what I’m doing wrong? Cause I have no idea. Thanks in advance, and I guess I’m buying a proper C++ book to get started properly.

  • 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-23T11:35:01+00:00Added an answer on May 23, 2026 at 11:35 am

    You need to make getData a const-method:

     const string CValue::getData() const {
         return *value;
     }
    

    Also, as your class looks now, it is not necessary to make value a pointer. Just make it a member-object.

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

Sidebar

Related Questions

Just writing a little function here and need some optimisation help! All requests redirect
I have a bit of a little popup warning thing, and I'd like it
I'm doing a app like photo browser just like the Photo on iTOuch. And
I have a simple little osx app that just starts up an IKPictureTaker and
I am trying to use this in my page class. I only just started
I am a little OCD and this is driving me insane. I have been
I'm just putting together a little 'hello world' type plugin to add a widget
OK, I'm a little confused. It's probably just a triviality. I've got a function
So here's what I'm trying to do in a little nutshell, I'm just gonna
A little while ago I noticed some Soap errors emitting from my app and

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.