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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:07:53+00:00 2026-06-12T19:07:53+00:00

I am a college student making the switch from java to c++ . We

  • 0

I am a college student making the switch from java to c++ . We have been introduced to overloading operators which I get for the most part, but for my assignment I have been stumped. The main asks for:

 Weight w1( -5, 35 ), w2( 5, -35 ), w3( 2, 5 ), w4( 1 ), w5, w6;
 //later on in main
 cout << "(w5 = 0) is " << ( w5 = 0 ) << endl;

My weight objects hold two ints, one for pounds one for ounces. When my program gets to this line w5 is already set to (0,0) however I feel like I’m returning the address as I get a massively long number when I print w5. Here is the code I have in .h and .cpp for the specific overload of =

//Weight.h
Weight& operator=(const int);

//Weight.cpp
Weight& Weight::operator=(const int number)
{
Weight changer(number); //constructs weight object with one int to (int, 0)
return changer;
}

I have learned from forums that I can’t make = a friend overload which has allowed me to take in 2 args for the function. Any help is greatly appreciated!

//code for my << overload
ostream& operator << (ostream& output, const Weight& w)
{
switch(w.pounds)
{
    case 1:
    case -1:
        output << w.pounds << "lb";
        break;
    case 0:
        break;
    default:
        output << w.pounds << "lbs";
        break;
}
switch(w.ounces)
{
case 1:
case -1:
    output << w.ounces << "oz";
    break;  
case 0:
    break;
default:
    output << w.ounces << "ozs";
    break;
}

if (w.pounds == 0 && w.ounces == 0)
{
    output << w.ounces << "oz";
}

return output;
}
  • 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-12T19:07:55+00:00Added an answer on June 12, 2026 at 7:07 pm
    //Weight.cpp
    Weight& Weight::operator=(const int number)
    {
       Weight changer(number); //constructs weight object with one int to (int, 0)
       return changer;
    }
    

    This is wrong, you are returning a reference to a temporary object. This object goes out of scope, and then you have your incorrect results.

    Generally, operator= is assignment, meaning you want to change the state of the object itself. It should therefore generally look like this:

    //Weight.cpp
    Weight& Weight::operator=(const int number)
    {
       // whatever functionality it means to assign the number to this object
       return *this;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a website (my college website) for which each student has a username/password
I am a college student and have been asked to familiarize myself with PCCTS,
I am making a website for my college in which students will have to
I'm a college student and this is my first time I have ever created
I am a college student slightly new to web programming and lately I have
I manage a Drupal 7 based website for my College's Student's Association. Most of
Suppose I have a relationship 1 to N, for example Student , College. Student
I am a college student. In our college we have to develop a simple
I'm a computer science college student. Yesterday, I have a class about Binary Search
I have recently created a student organization website (i.e. www-sc.xxx.edu/studentorganizations/ourorganization). Our college does not

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.