c++ - What is wrong with my syntax in this 1 line bit of code (pointers and references and dereferences oh my)? -


the code having trouble line:

result.addelement(&(*(setarray[i]) + *(rhs.setarray[j]))); 

the + operator in class overloaded (there variety of overloads can fit in set, have similar header):

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

the setarrays in code above both arrays of pointers, + operator requires references, might problem.

addelement, method of result, has header:

bool set::addelement(multinumber* newelement) 

the multinumber* in header parent class of rational, mentioned above. don't think of specific code matters. i'm pretty sure syntax issue.

my compiler error is:

68: error: invalid conversion 'const multinumber*' 'multinumber*' 

thank help!

this code has more serious issues can fix adding const or typecast somewhere.

the result of code crash somewhere down line, because you're passing pointer temporary. once finish line of code calls addelement, pointer left dangling, , trying use object points either result in nonsense (if you're reading object) or stack corrpution (if you're writing object).

the best way redefine code change

bool set::addelement(multinumber newelement) //pass multinumber value 

and call addelement follows:

result.addelement(*setarray[i] + *rhs.setarray[j]); 

note eliminated of parentheses because * has lower precedence [], parentheses around setarray[i] , setarray[i] redundant. think code more readable way.


well really, if can guess what's going on here, setarray internal storage of set class, it's type need redefined multinumber** multinumber*, in case call should be

result.addelement(setarray[i] + rhs.setarray[j]); 


edit ugggh. none of above allow keep polymorphism. need call new rational somewhere, , reasonable place can think of is:

result.addelement( new rational(*setarray[i] + *rhs.setarray[j]) ); 

this work without having redefine set::addelement.


a better solution redesign whole thing doesn't depend on polymorphism numeric classes (because numeric classes shouldn't wrapped in pointers in normal use).


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -