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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:51:09+00:00 2026-06-01T19:51:09+00:00

I am doing some program, and so far so good when it’s about implementation,

  • 0

I am doing some program, and so far so good when it’s about implementation, however now I am stuck with trivial problem, but I am in no position to find a solution for it. The problem is in this part of the code, and it say

Error 1 error C2662: ‘Smetler::action’ : cannot convert ‘this’ pointer from ‘const Smetler’ to ‘Smetler &’

Anyone knows what’s the problem here is, since I am sure I applied all what it was been said.

  virtual void action()
  {
    std::cout << "I'm a copy" << copy() << ". Doing observations." << std::endl;
  }      
  Smetler* copy() const { return new Smetler (*this); }     
   private:
   void writeDown(ostream& wd) const                            
   { 
      wd << Worker::getOccupation() << " " << Worker::getName() << ',' <<  Worker::getPosition()  << action(); 
   }
   };

Thanks in advance.

  • 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-01T19:51:11+00:00Added an answer on June 1, 2026 at 7:51 pm

    You have this:

    Smetler* copy() const { return new Smetler* (*this); }    
    

    This doesn’t allocate a Smetler object. It allocates a pointer of type Smetler. You’re attempting to convert a const Smetler& (which is the type of *this in const functions) to a Smetler*, which of course doesn’t make a whole lot of sense.

    What you probably want is this:

    Smetler* copy() const { return new Smetler(*this); }
    

    The above will allocate a new Smetler on the free store and copies the this object into the new space. You have to delete the returned pointer eventually to avoid memory leaks.

    What you really want is to use a smart pointer so you won’t have to worry about delete-ing the returned pointer from copy(). In C++03, you can use std::auto_ptr (although it has been deprecated since it can accidentally be used in an unsafe situation e.g. you can’t use auto_ptrs in containers like std::vector):

    std::auto_ptr<Smetler> copy() const
    {
        return std::auto_ptr<Smetler>(new Smetler(*this));
    }
    

    Or, if you can use C++11, use the much more superior std::unique_ptr which doesn’t have any of auto_ptr‘s problems.

    std::unique_ptr<Smetler> copy() const
    {
        return std::unique_ptr<Smetler>(new Smetler(*this));
    }
    

    Both the above code snippets will help a long way with preventing memory leaks (and not having to worry about them in the first place!)

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

Sidebar

Related Questions

I'm doing my best to learn DDD, but I'm unsure about some really basic
While doing some random experimentation with a factorial program in C, Python and Scheme.
I am doing some input/output between a c++ and a python program (only floating
I'm doing some random experimentation and want to print out the address the program
My program has been working perfectly so far, but it turns out that I've
Ok so I'm doing some research on buffer overflows. I've got a C program
I just started doing jQuery last week, and so far I already made some
Doing some jquery animation. I have certain divs set up with an attribute of
Doing some homework here (second assignment, still extremely green...). The object is to read
While doing some JavaScript performance tests I came up with the following piece of

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.