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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:03:00+00:00 2026-06-05T15:03:00+00:00

I am trying to write a copy constructor for a class but I get

  • 0

I am trying to write a copy constructor for a class but I get these two error messages, which I cannot decipher. Can somebody please tell me what I am doing incorrect?

class Critter
    {

    public:
        Critter(){}
        explicit Critter(int hungerLevelParam):hungerLevel(hungerLevelParam){}
        int GetHungerLevel(){return hungerLevel;}

        // Copy Constructors
        explicit Critter(const Critter& rhs);
        const Critter& operator=(const Critter& rhs);

    private:
        int hungerLevel;
    };

Critter::Critter(const Critter& rhs)
    {
    *this = rhs;
    }

const Critter& Critter::operator=(const Critter& rhs)
    {
    if(this != &rhs)
        {
        this->hungerLevel = rhs.GetHungerLevel(); // ERROR: object has type qualifier not compatible with member function
        }
    return *this;
    }

int _tmain(int argc, _TCHAR* argv[])
    {
    Critter aCritter2(10);
    Critter aCritter3 = aCritter2; // ERROR : No suitable copy constructor
    Critter aCritter4(aCritter3);
    return 0;
    }
  • 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-05T15:03:02+00:00Added an answer on June 5, 2026 at 3:03 pm

    You need int GetHungerLevel() const {return hungerLevel;}

    Otherwise, you aren’t allowed to call GetHungerLevel() using the reference-to-const rhs.

    Also, you can’t do copy-initialization with an explicit copy constructor, only direct initialization: Critter aCritter3(aCritter2);

    Probably you want to make the copy ctor non-explicit rather than necessarily change the definition of aCritter3, but your call.

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

Sidebar

Related Questions

I am trying to write a copy method which can be used to copy
this is my code (trying to write a copy constructor for my class): public
I'm trying to write a basic synchronisation program to copy files between two directories.
Trying to write a chat, like on facebook, I wondered if two clients can
I'm trying to write an extension method that I can use to copy values
I am trying to write a script which will automate a copy/paste of employee
I'm trying to write a copy constructor for an object managing a STL map
I'm trying to write a simple file uploader which can resize an image if
I am trying to write some code which will open List1.txt and copy the
I'm trying to write a jquery funciton that will copy specified fields from 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.