More C++ help, please!

notfred

Lifer
Feb 12, 2001
38,241
4
0
addwump->set_x(17); <--- addwump is a pointer to a Wumpus object

void Wumpus::set_x(double x_init){
x = x_init;
}


that's my function (member of class Wumpus)

x is a private double in the class.

At runtime, I get an error about trying to write to address 00000000.
the compiler traces it to the line: x = x_init;

Any ideas?
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
the last time I set addwump before calling set_x was:
addwump = new Wumpus("Jerry",3,8);
 

Mucman

Diamond Member
Oct 10, 1999
7,246
1
0
You mean

Wump* addwump = new Wumpus("Jerry",3,8);

right?

int main()
{
Wump* addwump;
addwump->set_x(17);
return 0;
}

the results in error

int main()
{
Wump* addwump = new Wump();
addwump->set_x(17);
delete addwump;
return 0;
}

Does not result in error. I created a class Wump with a default constructor and the set_x method.

<edit>Better delete my pointers before I get laughed at </edit>
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
void main(){
WumpusList MainList;
Wumpus * addwump = new Wumpus("Bob",2,3);
MainList.update(addwump);
addwump->set_x(17);
addwump->set_y(43);
MainList.update(addwump);
cout << "POPULATION: " << MainList.GetPopulation() << "\n";
MainList.PrintList();
char z;
cin >> z;
}


that's my main in entirety. It's still failing on the set_x call.

also:

Wumpus extrawump("Bob",2,3);
extrawump.set_x(17);

works fine.
 

Mucman

Diamond Member
Oct 10, 1999
7,246
1
0
Does the MainList object just grab all the attributes from the Wumpus object that the pointer that's passed to it points to? Can I see the code for you constructor? I know it shouldn't matter, but I am confused now
 

dyqith

Member
Feb 6, 2000
93
0
0
hey, can i see the whole class definition...

it seems like you might be missing memory space (?) in the instance...

--Dyqith
 

Ameesh

Lifer
Apr 3, 2001
23,686
0
0
ive never seen an array passed liked that, i dont think you can do it, change it to a char* to pass it around in functions, what kind of freakish compiler are you using?
 

dyqith

Member
Feb 6, 2000
93
0
0
hmm...
i d/l the file, compiled and ran it...
it works for me...

gives me the output :
POPULATION: 1
Bob: X=2 Y=3


maybe you are running low on memory ? (wild guess)
--dyqith
 

notfred

Lifer
Feb 12, 2001
38,241
4
0


<< hmm...
i d/l the file, compiled and ran it...
it works for me...

gives me the output :
POPULATION: 1
Bob: X=2 Y=3


maybe you are running low on memory ? (wild guess)
--dyqith
>>



Uncomment the 3 lines in question

(I updated the link, they're now uncommented on the website)
 

Mucman

Diamond Member
Oct 10, 1999
7,246
1
0
dyqith - He has commented out the naughty code

notfred - I just tried creating the addwump object using the default constructor and I was able to use the set_x method...
 

notfred

Lifer
Feb 12, 2001
38,241
4
0


<< ive never seen an array passed liked that, i dont think you can do it, change it to a char* to pass it around in functions, what kind of freakish compiler are you using? >>



I'm not having problems with strings, I'm having problems changing the value of a double.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0


<< notfred - I just tried creating the addwump object using the default constructor and I was able to use the set_x method... >>



What about creating it with the overloaded constructor?
 

Mucman

Diamond Member
Oct 10, 1999
7,246
1
0
notfred - remove the & from the update method of the WumpusList class I did that and it runs :

POPULATION: 2
Bob: X=17 Y=43
Bob: X=17 Y=43

That's what I get when I run it now. It is still not fixed, but we are getting there
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
well, crap, how come it work with that constructor?

It seems to work fine with the other two.
 

Bleepeep

Member
Jul 9, 2001
133
0
0
asked my friend and he says:



i donno what the prob is but instead of doing

Wumpus * addwump = new Wumpus("Bob",2,3);

why don't you do something like:

Wumpus addwump("Bob",2,3);
MainList.update(&addwump);
addwump.sex_x(17);
etc...

you're dynamically allocating memory space for the class, seems kinda odd. You might have to change a bunch of code, I don't know
 

notfred

Lifer
Feb 12, 2001
38,241
4
0


<< notfred - remove the & from the update method of the WumpusList class I did that and it runs :

POPULATION: 2
Bob: X=17 Y=43

That's what I get when I run it now.
>>



I need to be able to delete the user's pointer to the new wumpus object in my linked list though. hmm...
 

notfred

Lifer
Feb 12, 2001
38,241
4
0


<< asked my friend and he says:



i donno what the prob is but instead of doing

Wumpus * addwump = new Wumpus("Bob",2,3);

why don't you do something like:

Wumpus addwump("Bob",2,3);
MainList.update(&addwump);
addwump.sex_x(17);
etc...

you're dynamically allocating memory space for the class, seems kinda odd. You might have to change a bunch of code, I don't know
>>



Why would I make a linked list without dynamically allocating memory?
 

Mucman

Diamond Member
Oct 10, 1999
7,246
1
0
I think the program is haulting in this area :

ptr->set_x(w->get_x());
ptr->set_y(w->get_y());
w = 0;

which is in the update method of WumpusList
 

dyqith

Member
Feb 6, 2000
93
0
0
damn... should have been reading the entire code

hmm, i traced it down to the line:
Wumpus * ptr = FindWumpus(w->get_name()); from the function bool WumpusList::update(Wumpus * w)

i changed it so instead of passing by reference (i think that's what you were trying to do), i let it pass by pointer, hence there's no & in the above args of update...


that fixed it up..

--Dyqith

Update:
Okie, i must be the slowest typer here... you guys handle this, i dont' even know what the program is for...

i'm goin to go back to my own program...

anybody want to help me in Prolog ?
--dyqith
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
if (ptr!=0){
double x = w->get_x();
double y = w->get_y();
ptr->set_x(x);
ptr->set_y(y);
w = 0;
return 1;




I changed it to that, and it's working now. Thanks for your help everyone, especially Mucman
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |