-
Problem
I'm still quite a newbie to C++, but definately not to C. I'm working on my first large C++ project, and I've made a lot of headway with it, but I'm encountering an error I just cannot figure out. It compiles fine, but I'm getting an invalid page fault error in the output of my "term" class. Judging from the error and past experience, I'd say I messed up with new and delete allocation. I'm basing all my work off of a pretty shabby C++ tutorial, though. I'm trying to find a new one. Anyway, my two source files are attached. I've marked the point I narrowed down the error to, but I've come to find that that sometimes doesn't mean squat, as I'm sure many of you know.
-
Ugg.. its very late here.. i'm half asleep.. plus rarely use double pointers **.
nvalid page fault error Would this occur if you were trying to access memory that your program doesn't have access to? :confused:
Anyways from what I see the number variables aren't being initiazed correctly in your term class.
Code:
numer = new number*[1];
denom = new number*[1];
Code:
number** temp;
temp = new number*[numercount];
Shouldn't temp,numer,denom after this statement point to a number with initial values set to BASIC_ARGUMENTS? I'm getting bumbkiss.
This doesn't seem to initialize anything correctly. Thus preventing the following section of code from working
Code:
term operator *= (number& numptr)
{
++numercount;
number** temp;
temp = new number*[numercount];
for(int i; i < numercount - 1; ++ i)
temp[i] = numer[i];
delete [] numer;
numer = new number*[numercount];
for(int x; x < numercount - 1; ++x)
numer[x] = temp[x];
numer[numercount - 1] = &numptr;
delete [] temp;
return *this;
}
Honestly I'm completely confused.. why would you initialize numer[0] = NULL then try and copy it over to a temp structure?
As for the line in red don't you need to set int i = 0? Otherwise i will equal like -89328409823.
As for the next block of code:
Code:
ostream& operator << (ostream& out, term &t)
{
cout << endl;
// cout << "Checkpoint 2" << endl;
for(int i = 0; i < t.numercount; ++i)
{
cout << "Checkpoint 3" << endl;
cout << " Numerator " << (i + 1) << " address = " << int(t.numer[i]);
cout << endl;
}
cout << endl;
for(int x = 0; x < t.denomcount; ++x)
{
cout << "Checkpoint 4" << endl;
cout << " Denominator " << (x + 1) << " address = " << int(t.denom[x]);
cout << endl;
}
// cout << "Checkpoint 5" << endl;
Shouldn't you put:
or something like that as the last line.. i think the function wants to return a stream.
Oh yeah i made it through here was my final output.
Code:
num address = 4695928
num2 address = 4695952
Checkpoint 1
Checkpoint 3
Numerator 1 address = -572662307
Checkpoint 5
Checkpoint 6
Press any key to continue...
Code:
~term()
{
delete [] numer;
delete [] denom;
}
Seems rather hostile as well :D
Therefore
Code:
ostream& operator << (ostream& out, term t)
When this function ends t will call the destructor. According to your code t points to num which points to a number. So T is destroyed which eliminates NUM? I THINK.. so after main() when T is destroyed again KABOOM!
Lastly:
After this line T is automatically destroyed.. thus wasted all of the effort made to multiply it! why? cause T isn't being passed in by reference.
Code:
BEFORE
term operator *= (number& numptr)
AFTER
term& operator *= (number& numptr)
FINAL OUTPUT
Code:
num address = 4695928
num2 address = 4695952
Checkpoint 1
Checkpoint 2
Checkpoint 3
Numerator 1 address = 4695928
Checkpoint 5
Checkpoint 6
Press any key to continue...
Anyways like I said I don't use ** very much ... so i'm probably just giving you completely wrong info.. but oh well...can't say i didn't try :D
-
Post your code between code tags if you want more people to look at it.
-
*sigh* I always make stupid maistakes like the uninitializing of int i, forgetting to return cout, and the missing & indentifier. Again, I'm still a newb, so these things aren't habit, yet. Sorry for making you go through all that.
I think you were confused about the initialization thing. I realize my programming is uite difficult to understand. I very rarely need to put the entire thing up, so others being able to understand it isn't an issue. I'll have to work on that. The basic idea is that a term will be produced when two numbers are put together through multiplication or division, and the numebers can't go together. Usually, this would mean that the exponents are different. Thus, when a term is created, it doesn't know which numbers it should point to. It's basic algebra, when you think about it, except with no variables. I initialize them to NULL, simply to make sure that they don't point to anything. If you think that the *= operator is copying the NULL over, that's becuase I haven't written the part that will actually change that NULL. But in any case, I'll go back over my thinking on that part.
I use double pointers, by the way, simply so I don't have to create a whole new number variable for each term. I may end up rethinking that.
Anyway, thanks a whole lot for your input. Sorry you had to go through all this, especially at night. I know what it's like dealing with code at three in the morning. :P I'll bet that uninitialized i was causing me to access the wrong memory. Thanks again.
-
For your overloaded << operator, you should be outputting to out (the stream passed as an argument) and then return that as well.