+ 3

Issue with referencing a value [ Solved ]

I created two classes one Super or base class and one derived. I want to pass a value to the derived class's level property but my function returns a memory address. If you know what I did wrong, I would appreciate a hint on how the solve the issue. here is the code. https://code.sololearn.com/c83Sbe5wckt9/?ref=app

10th Jul 2017, 1:48 AM
Manual
Manual - avatar
8 Réponses
+ 7
I fixed it for you, your setLevel function was wrong: https://code.sololearn.com/cglcY259paM4/?ref=app
10th Jul 2017, 1:51 AM
Karl T.
Karl T. - avatar
+ 8
You made some mistakes with the setlevel() method in derived class. It should be: class LightningDragon:public Dragon { public: void setLevel(int l){ level = l; } LightningDragon(int l){ setLevel(l); } // ... };
10th Jul 2017, 1:54 AM
Hatsy Rei
Hatsy Rei - avatar
+ 5
You declared int setLevel inside the constructor, and in the function lower down you declared a level variable inside setLevel(), instead of setting the value to the "level" member.
10th Jul 2017, 2:04 AM
Karl T.
Karl T. - avatar
+ 2
@Karl T Thank you! I am reviewing the code. Where is the issue with the setLevel(); ? @Hatsy answered it already
10th Jul 2017, 2:02 AM
Manual
Manual - avatar
+ 2
@Hatsy Thank you! it makes sense a set function should not return the value. Then it must have been returning addresses or random values
10th Jul 2017, 2:04 AM
Manual
Manual - avatar
+ 1
The code is large, so I left comments in the main on the getLevel(); function. The function giving me trouble.
10th Jul 2017, 1:41 AM
Manual
Manual - avatar
+ 1
I am trying the edit the set and get functions to get the result I want.
10th Jul 2017, 1:42 AM
Manual
Manual - avatar
+ 1
Lol Thanks again, spent a lot of time trying to find a solution. I did not see the extra " int" derailing the function.
10th Jul 2017, 2:13 AM
Manual
Manual - avatar