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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:42:57+00:00 2026-05-18T07:42:57+00:00

The code that I am having trouble with is this line: result.addElement(&(*(setArray[i]) + *(rhs.setArray[j])));

  • 0

The code that I am having trouble with is this line:

result.addElement(&(*(setArray[i]) + *(rhs.setArray[j])));

The + operator in my class is overloaded like this (there are a variety of overloads that can fit in this set, but they all have a similar header):

const Rational Rational::operator+(const Rational &rhs) const

The setarrays in the code above are both arrays of pointers, but the + operator requires references, which might be the problem.

AddElement, the method of result, has this header:

bool Set::addElement(Multinumber* newElement)

The Multinumber* in the header is the parent class of Rational, mentioned above. I don’t think any of the specific code matters. I’m pretty sure that it is a syntax issue.

My compiler error is:

68: error: invalid conversion from 'const Multinumber*' to 'Multinumber*'

Thank you for your help!

  • 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-18T07:42:58+00:00Added an answer on May 18, 2026 at 7:42 am

    This code has much more serious issues than you can fix by adding a const or a typecast somewhere.

    The result of this code will ultimately be a crash somewhere down the line, because you’re passing a pointer to a temporary. Once you finish with line of code that calls addElement, the pointer will be left dangling, and trying to use the object it points to will either result in nonsense (if you’re reading the object) or stack corrpution (if you’re writing to the object).

    The best way to redefine your code would be to change this to

    bool Set::addElement(Multinumber newElement) //pass the Multinumber by value
    

    and call addElement as follows:

    result.addElement(*setArray[i] + *rhs.setArray[j]);
    

    Note that I eliminated all of the extra parentheses because * has lower precedence than [], so the parentheses around setArray[i] and setArray[i] were redundant. I think the code is more readable this way.


    Well really, if I can guess what’s going on here, setArray is the internal storage of the Set class, so it’s type will need to be redefined from Multinumber** to Multinumber*, in which case the call really should be

    result.addElement(setArray[i] + rhs.setArray[j]);
    


    EDIT Ugggh. None of the above will actually allow you to keep your polymorphism. You need to call new Rational somewhere, and the only reasonable place that I can think of is:

    result.addElement( new Rational(*setArray[i] + *rhs.setArray[j]) );
    

    This will work without having to redefine Set::addElement.


    A better solution would be to redesign the whole thing so that it doesn’t depend on polymorphism for numeric classes (because numeric classes really shouldn’t be wrapped in pointers in most normal use).

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

Sidebar

Related Questions

Would having a nice little feature that makes it quicker to write code like
The code that I want to write is like this: void MethodOnThreadA() { for
I'm having trouble creating a port in Unix. This code keeps returning Error creating
I am modifying some code and came across a declaration that I am having
I'm maintaining some code that uses a *= operator in a query to a
I'm having trouble getting a Java Applet to communicate with the Javascript code on
I'm having trouble with this XML to CSV Script.. when I remove the headers
Alright, I'm having trouble getting started on this one. I'm trying to use the
Hey, this is probably a simple question, but I am having trouble running my
I'm having trouble with my program crashing. I get that Program.exe has stopped working

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.