| INeedAHero (29) | |||||
|
Okay, so I have to create a lab that adds and multiplies giant integers that are 40 digits long. I have to do this by essentially creating an array of 40 digits and writing code that imitates multiplying and adding. I had everything working, with classes and everything. Except, apparently, I'm supposed to be passing OBJECTS through my functions as parameters, not arrays. In addition, I'm supposed to implement operator overloading. I tried doing this and I can't even come remotely close to compiling. I just don't understand the purpose of objects and how to use them. Here's my current (and absolutely horrible) .cpp
And here's my .h
Obviously, I don't expect any of you to correct even a tenth of the slew of problems here. But can you at least show me the correct way to use an object? I mean, I had the ENTIRE THING working without using objects (but with a .h and a .cpp - that is, with classes), so I can't imagine that objects make it that much more complicated, you know? This is my first time posting here, so I apologize in advance if I have made any errors in posintg. Thank you all for your time and help. | |||||
|
|
|||||
| Chervil (1203) | |
|
Just a very quick glance at your HugeInteger class shows it contains no less than three arrays of integers. That seems like probably the wrong starting point. I would expect there to be just a single array, which represents the value of the huge integer. (There might also be a separate sign, if you need to represent positive and negative values). So I would start by trimming down the class, and then reconsidering the rest of the code. | |
|
|
|
| freddy92 (220) | |||
|
To add on to that, you want to think of each array as just one part of an expression (like x = y + z). x, y, and z would all be separate HugeInteger objects, each with only one array (not 3). In your constructor, you cannot initialize the array like that. Just use the for loop you have commented out, but remember that there will be only ONE array. For operator +, you already have the declaration for it. You just need to basically take your code in add(...), put it in operator+, and make it work for your object with one array. You should be declaring a new HugeInteger to store the result, as you are doing. Let's assume the paramater is called "right" (because, in y + z, y is the "invoking object" and z is the parameter passed to y's operator+ function). Finally lets assume that the array in each HugeInteger is called "arr". Taking as an example line 33, it would now look like:
For operator *, assuming it all works correctly as you say, you can just take the mult function and modify it as I described for operator+. | |||
|
Last edited on
|
|||
| INeedAHero (29) | |||||
|
Yes, my professor recommended that as well. And I changed it to that, but then I got confused... You see, if I'm adding two numbers together, and getting a result, that's three different numbers, each represented by an array, and I don't see how it's possible to do that while working with just a single array. I can guess that I will do this be creating three objects? Well, let's say I alter my .h code into this:
But how, then, does my .cpp account for this? How does it use the HugeInteger object? To keep things simple, let's ignore the complicated multiplication function and just focus solely on the simple additon function and the constructor:
You see that bolded line "object.result[i] = object.op1[i] + object.op2[i] + carry;"?? What is that supposed to look like when there is only one array being used? | |||||
|
|
|||||
| freddy92 (220) | |
| Read my reply above your new post. | |
|
|
|
| INeedAHero (29) | |||||
|
Thank you for all the replies! I read your post carefully, and I assume you meant for me to banish the add(...) function altogether and just copy paste into the operator+ function (which seems to make sense to me). Howeve, when I did that, along with the other changes you recommended, the compiler still gave errors. The compiler said some of the problems were in the .h file, which I did modify slightly, so I'll repost it: Here's my slightly modified .h file:
And here's my constructor and add function:
I tried pretty hard to find out what was wrong here but I couldn't see anything. The compiler is talking about an invalid attempt to attach a return value to the constructor function....but I'm not returning anything. I'm at a loss here. | |||||
|
|
|||||
| freddy92 (220) | |
| Well, you should be returning result at the end of operator+. Otherwise, what's the point of the function? Also it's more helpful if you just copy paste the entire error it gives you. | |
|
|
|
| INeedAHero (29) | |
|
Ah, of course. I added that in, thank you. And here's the error: HugeInteger.h:15:1: error: new types may not be defined in a return type HugeInteger.h:15:1: note: (perhaps a semicolon is missing after the definition of âHugeIntegerâ) HugeInteger.cpp:11:26: error: return type specification for constructor invalid | |
|
|
|
| freddy92 (220) | |
| The note it gives you is exactly right; you don't have a semicolon after the definition of your class. Add one after the '}' on line 23. As for the other error, please just copy paste the whole .cpp file. The error could be a result of something before the line it claims the error is on. Don't be afraid of it being too long. | |
|
Last edited on
|
|
| INeedAHero (29) | |||||
|
Haha, I feel silly. I thought too deeply into it, I suppose. And to avoid compilation errors, I actually turned the rest of my .cpp into a comment. I plan to undo it when the I nail down the add function. Regardless, I was able to compile after putting in that semicolon. Thank you so much! After fixing some more errors, I was also to make my program compile with the tester.cpp. Now I think there's (hopefully) only one last loose end to tie up. I don't understand how to use a constructor with parameters in this kind of situation. Here's my HugeIntMain.cpp file (the one that I actually run the program with):
The idea is for me to add these two arrays together - the ones defined here have real numbers in them. I just don't know how to get them into my functions correctly, you see what I mean? And here's the entire .cpp file, minus just the commented sections: You'll see that the bolded section is not correct (that is, I'm pretty sure it isn't).
| |||||
|
|
|||||
| freddy92 (220) | |||||
Here's what your constructor should be:
Here's how you would use it:
You could add a function that lets you set a HugeInteger object equal to a new array, instead of making a new one every time. | |||||
|
|
|||||
| INeedAHero (29) | |||||
|
I made the changes you recommended, and it wouldn't compile - it seems that I had to type the parameter as "HugeInteger::HugeInteger(int op[])", that is, with the bracket at the end of the variable name. After that it compiled - thank you! And I considered adding the object-to-array function as you recommended, but I think that ties in with the fact that I will ultimately be receiving the arrays as integers from the user. So I'll cross that bridge when I get to it. For now, I'm just trying to successfully implement the operator overloading. Based on what you said, I rewrote my code like this: Main function
This all compiles, but it claims the sum is zero when I run the program! I examined the code carefully, but I can't see why it isn't adding the two arrays together. At the very least, it should be displaying a value of 4027 (op1 without being added to op2), but it is doing nothing at all! I have two constructors (default and one with parameters) and they look like this:
I made no other changes to my program. Any ideas on what's wrong here? | |||||
|
|
|||||
| AbstractionAnon (516) | ||||||||
|
I get a warning in your + operator that carry is uninitialized. Reference lines 17 and 21 in your post (3 back). This will cause unpredictable results. Also at line 25 of your main, object is being displayed. That's not the result. With those changes, I get the correct answer.
I added an overload of the << operator so HugeInteger acts like any standard c++ type:
Corrected + opertator:
main.cpp
edit: to clarify reference to previous post. | ||||||||
|
Last edited on
|
||||||||
| INeedAHero (29) | |||
|
Thanks! I did what you said and after playing around a little I got it working. I also tried using the same logic to alter my multiplication function, and I'm pretty sure I got everything converted correctly, except there's one little hiccup that I'm not sure how to handle. You see, as a part of the multiplication function I must add two numbers together (when you do the multiplication longhand you see what I mean). Obviously, I need to call the add function for that. I'm not sure how I would go about that, since I can only add two objects together and in the multiplication function itself I'm really dealing only with arrays. The really confusing part is that my array "mult_array[]" is what I need to add...but I can't add with it because it's not an object. Do you see what I'm saying? Here's my newly altered multiplication file and the addition function I am trying to use: (look at the bolded section to see where the problem is - line 66)
| |||
|
Last edited on
|
|||
| INeedAHero (29) | |
| Anyone? | |
|
|
|
| AbstractionAnon (516) | ||||||||||
So convert the array to an object. :) Actually, what I would do would be to create a private helper function that adds two arrays. The helper can then be called by both the + and * operators. You want the helper to be private since the internal representation of the array is private. I also just noticed one thing about both your + and * operators. Both return a HugeInteger by value. When one defines an arithmetic operator, it is normal to both modify the instance and to return the instance by reference. Your declarations should look like this:
This is more efficient that returning by value. Returning by value requires creating a temporay instance and copying the contents to the temporary instance. Returning by reference simply returns a pointer. You'll need to change both functions to return a self-reference:
You also have a couple of errors in the code in your most recent post. 1) Line 39:
You're referencing right.arr, while the argument passed in at line 24 is rightside.2) Line 66:
i is undefined. i is only defined within the scope of the preceding for loop. | ||||||||||
|
|
||||||||||
| INeedAHero (29) | |
|
Thank you for the observations. So, if I understand you correctly, you're saying I should move the contents of operator+ into its own add(...) function, and then merely have operator+ call that add(...) function, and also have operator* call that function when necessary? And would I be correct to deduce that the operator+ function deals with objects, whereas the add(...) function deals purely with arrays? | |
|
|
|
| AbstractionAnon (516) | |
|
Yes on both counts. | |
|
|
|
| INeedAHero (29) | |||||
|
Ok, what you said made a lot of sense. And then I tried doing it and got extremely confused haha. I'm not sure how to pass the array, seeing as how my variable arr is private and thus cannot be accessed by objects with the dot operator. I doubt that the solution would be to make it public. Here's my modified .h
And here's my operator+ along with my add(...) -- this is where I'm having trouble.
| |||||
|
|
|||||
| AbstractionAnon (516) | |||
|
A few things: 1) The declaration for your + operator doesn't match your implementation. The declaration is correct. You need to fix the implementation to match. hint:
2) Again for your add function, your declaration and implementation don't match. This time, it's your implementation that is correct. You might think about whether you want add to use and return the result in op1 or operate on the instance's arr[] (i.e. take only one argument). 3) Line 11 in your header. You can't input to a const instance. | |||
|
|
|||