Error in returning array of int as reference
-
Hi, i have to ask a question about some codelines, where i have a mistake, because the array of int always returns 1 for every instance and i spent a lot of hours and still cannot find the fail i have maid. I simply post the necessary codelines in hope that someone can put away the wood infront of my head. Inside a modal dialogue, these are the calls
for (int y=0;y<5;y++)
{
Schadensnummer[y]=KlendautKlendaut_Gegner[0].Basistrefferermittlung();
}The class "KlendautKlendaut_Gegner0" - function Basistrefferermittlung:
int CBasis::Basistrefferermittlung()
{
int Zufallstreffer;
Zufallstreffer=rand()%99+1;
int Trefferchance;
Trefferchance=rand()%99+1;
static int Einschlagsnummer;if (Trefferchance>80) { if (Zufallstreffer>95) { if (m\_Bodyguard>1) { Einschlagsnummer=20; m\_Bodyguard=m\_Bodyguard-1; return (Einschlagsnummer); } else if (m\_Bodyguard<1&&m\_Palast>1) { Einschlagsnummer=21; m\_Palast=m\_Palast-1; return (Einschlagsnummer); } } else if (Zufallstreffer>90&&Zufallstreffer<95) { if (m\_Fabrik>1) { Einschlagsnummer=16; m\_Fabrik=m\_Fabrik-1; return (Einschlagsnummer); } } else if (Zufallstreffer>85&&Zufallstreffer<90) { if (m\_Scanner>1) { Einschlagsnummer=17; m\_Scanner=m\_Scanner-1; return (Einschlagsnummer); } } else if (Zufallstreffer>80&&Zufallstreffer<85) { if (m\_Kraftwerk>1) { Einschlagsnummer=18; m\_Kraftwerk=m\_Kraftwerk-1; return (Einschlagsnummer); } } else if (Zufallstreffer>75&&Zufallstreffer<80) { if (m\_Zentrale>1) { Einschlagsnummer=19; m\_Zentrale=m\_Zentrale-1; return (Einschlagsnummer); } } } if (m\_Schutzwall>1) { Einschlagsnummer=1; m\_Schutzwall=m\_Schutzwall-1; return (Einschlagsnummer); } else if (m\_Gefechtsstand>1) { Einschlagsnummer=2; m\_Gefechtsstand=m\_Gefechtsstand-1; return (Einschlagsnummer); } else if (m\_Geschuetzturm>1) { Einschlagsnummer=3; m\_Geschuetzturm=m\_Geschuetzturm-1; return (Einschlag
-
Hi, i have to ask a question about some codelines, where i have a mistake, because the array of int always returns 1 for every instance and i spent a lot of hours and still cannot find the fail i have maid. I simply post the necessary codelines in hope that someone can put away the wood infront of my head. Inside a modal dialogue, these are the calls
for (int y=0;y<5;y++)
{
Schadensnummer[y]=KlendautKlendaut_Gegner[0].Basistrefferermittlung();
}The class "KlendautKlendaut_Gegner0" - function Basistrefferermittlung:
int CBasis::Basistrefferermittlung()
{
int Zufallstreffer;
Zufallstreffer=rand()%99+1;
int Trefferchance;
Trefferchance=rand()%99+1;
static int Einschlagsnummer;if (Trefferchance>80) { if (Zufallstreffer>95) { if (m\_Bodyguard>1) { Einschlagsnummer=20; m\_Bodyguard=m\_Bodyguard-1; return (Einschlagsnummer); } else if (m\_Bodyguard<1&&m\_Palast>1) { Einschlagsnummer=21; m\_Palast=m\_Palast-1; return (Einschlagsnummer); } } else if (Zufallstreffer>90&&Zufallstreffer<95) { if (m\_Fabrik>1) { Einschlagsnummer=16; m\_Fabrik=m\_Fabrik-1; return (Einschlagsnummer); } } else if (Zufallstreffer>85&&Zufallstreffer<90) { if (m\_Scanner>1) { Einschlagsnummer=17; m\_Scanner=m\_Scanner-1; return (Einschlagsnummer); } } else if (Zufallstreffer>80&&Zufallstreffer<85) { if (m\_Kraftwerk>1) { Einschlagsnummer=18; m\_Kraftwerk=m\_Kraftwerk-1; return (Einschlagsnummer); } } else if (Zufallstreffer>75&&Zufallstreffer<80) { if (m\_Zentrale>1) { Einschlagsnummer=19; m\_Zentrale=m\_Zentrale-1; return (Einschlagsnummer); } } } if (m\_Schutzwall>1) { Einschlagsnummer=1; m\_Schutzwall=m\_Schutzwall-1; return (Einschlagsnummer); } else if (m\_Gefechtsstand>1) { Einschlagsnummer=2; m\_Gefechtsstand=m\_Gefechtsstand-1; return (Einschlagsnummer); } else if (m\_Geschuetzturm>1) { Einschlagsnummer=3; m\_Geschuetzturm=m\_Geschuetzturm-1; return (Einschlag
Use a debugger and step through the function. If a variable's value is not what you are expecting it is a lot easier to find out why by watching when it changes than by peering through the code looking for clues.
-
Use a debugger and step through the function. If a variable's value is not what you are expecting it is a lot easier to find out why by watching when it changes than by peering through the code looking for clues.
-
You need to delve deeper in the debugger - look at where the array elements are being set to 1, and you should see why (Use F11 to step instead of F10).
Cheers, Mick ------------------------------------------------ It doesn't matter how often or hard you fall on your arse, eventually you'll roll over and land on your feet.
-
You need to delve deeper in the debugger - look at where the array elements are being set to 1, and you should see why (Use F11 to step instead of F10).
Cheers, Mick ------------------------------------------------ It doesn't matter how often or hard you fall on your arse, eventually you'll roll over and land on your feet.
Hi, i made a step per step view on the code. It runs through "Schadensnummer[y]=RestonaGuldon_Gegner[0].Basistrefferermittlung();" and returns 1 after it doesn't enter the line it should
//...
if (m_Schutzwall>1)
{
Einschlagsnummer=1;
m_Schutzwall=m_Schutzwall-1;
return (Einschlagsnummer);
}
//...it should enter there, because the class array object has .m_Schutzwall>1. But it neither enters and sets Einschlagsnummer to 1 or subtracts the m_Schutzwall by 1. There must be something wrong with my declaration of the array of the class "RestonaGuldon_Gegner[0]" oder initialization of the function "Basistrefferermittlung()". But i can't see what it is. I'm blindfolded. I just did everything how i am used to do and it worked fine till now. I am stunned.
-
Hi, i made a step per step view on the code. It runs through "Schadensnummer[y]=RestonaGuldon_Gegner[0].Basistrefferermittlung();" and returns 1 after it doesn't enter the line it should
//...
if (m_Schutzwall>1)
{
Einschlagsnummer=1;
m_Schutzwall=m_Schutzwall-1;
return (Einschlagsnummer);
}
//...it should enter there, because the class array object has .m_Schutzwall>1. But it neither enters and sets Einschlagsnummer to 1 or subtracts the m_Schutzwall by 1. There must be something wrong with my declaration of the array of the class "RestonaGuldon_Gegner[0]" oder initialization of the function "Basistrefferermittlung()". But i can't see what it is. I'm blindfolded. I just did everything how i am used to do and it worked fine till now. I am stunned.
Nowhere in your code can we see the defintion of "m_Schutzwall" it would be useful Are you sure you don't have two, m_Schutzwall's it's quite common to that and easy to miss. If you turn the compiler up to W4 maximum warning it will tell you if m_Schutzwall is hidden by a local copy. It's dead simple to do something silly with scope like this .. try it
int m_Schutzwall = 1;
int j;
for (j = 1; j < 10; j++){
int m_Schutzwall = j + 2;// Now at this point there actually two m\_Schutzwall's and that is legal // The last m\_Schutzwall only exists in this scope // You are in for a shock if you expect m\_Schutzwall to be 1 in here
}
Do a search for in files for "m_Schutzwall" and look at all the uses.
In vino veritas
-
Nowhere in your code can we see the defintion of "m_Schutzwall" it would be useful Are you sure you don't have two, m_Schutzwall's it's quite common to that and easy to miss. If you turn the compiler up to W4 maximum warning it will tell you if m_Schutzwall is hidden by a local copy. It's dead simple to do something silly with scope like this .. try it
int m_Schutzwall = 1;
int j;
for (j = 1; j < 10; j++){
int m_Schutzwall = j + 2;// Now at this point there actually two m\_Schutzwall's and that is legal // The last m\_Schutzwall only exists in this scope // You are in for a shock if you expect m\_Schutzwall to be 1 in here
}
Do a search for in files for "m_Schutzwall" and look at all the uses.
In vino veritas
Is it possible that the array int Schadensnummer[] establishs a copy of the member variable of the class RestonaGuldon_Gegner[0]? The class is global, the variable m_Schutzwall is public. The class is capsulated in an array. I dont see a solution so far. it couldnt be that i have to give the array of the class in its own function. i think i forgot something, but i still cant see it. :(
-
Is it possible that the array int Schadensnummer[] establishs a copy of the member variable of the class RestonaGuldon_Gegner[0]? The class is global, the variable m_Schutzwall is public. The class is capsulated in an array. I dont see a solution so far. it couldnt be that i have to give the array of the class in its own function. i think i forgot something, but i still cant see it. :(
addition: i feel like an idiot. Maybe i am one. There must be everything allright. It just misses a ">=" in the function instead of a ">", because the variable is equal 1. So the entrypoint is never beeing reached. I ll change it and then we ll see. Thank you so far, i hope i just gave you an evening with something to smile about. Thanks.
-
Hi, i have to ask a question about some codelines, where i have a mistake, because the array of int always returns 1 for every instance and i spent a lot of hours and still cannot find the fail i have maid. I simply post the necessary codelines in hope that someone can put away the wood infront of my head. Inside a modal dialogue, these are the calls
for (int y=0;y<5;y++)
{
Schadensnummer[y]=KlendautKlendaut_Gegner[0].Basistrefferermittlung();
}The class "KlendautKlendaut_Gegner0" - function Basistrefferermittlung:
int CBasis::Basistrefferermittlung()
{
int Zufallstreffer;
Zufallstreffer=rand()%99+1;
int Trefferchance;
Trefferchance=rand()%99+1;
static int Einschlagsnummer;if (Trefferchance>80) { if (Zufallstreffer>95) { if (m\_Bodyguard>1) { Einschlagsnummer=20; m\_Bodyguard=m\_Bodyguard-1; return (Einschlagsnummer); } else if (m\_Bodyguard<1&&m\_Palast>1) { Einschlagsnummer=21; m\_Palast=m\_Palast-1; return (Einschlagsnummer); } } else if (Zufallstreffer>90&&Zufallstreffer<95) { if (m\_Fabrik>1) { Einschlagsnummer=16; m\_Fabrik=m\_Fabrik-1; return (Einschlagsnummer); } } else if (Zufallstreffer>85&&Zufallstreffer<90) { if (m\_Scanner>1) { Einschlagsnummer=17; m\_Scanner=m\_Scanner-1; return (Einschlagsnummer); } } else if (Zufallstreffer>80&&Zufallstreffer<85) { if (m\_Kraftwerk>1) { Einschlagsnummer=18; m\_Kraftwerk=m\_Kraftwerk-1; return (Einschlagsnummer); } } else if (Zufallstreffer>75&&Zufallstreffer<80) { if (m\_Zentrale>1) { Einschlagsnummer=19; m\_Zentrale=m\_Zentrale-1; return (Einschlagsnummer); } } } if (m\_Schutzwall>1) { Einschlagsnummer=1; m\_Schutzwall=m\_Schutzwall-1; return (Einschlagsnummer); } else if (m\_Gefechtsstand>1) { Einschlagsnummer=2; m\_Gefechtsstand=m\_Gefechtsstand-1; return (Einschlagsnummer); } else if (m\_Geschuetzturm>1) { Einschlagsnummer=3; m\_Geschuetzturm=m\_Geschuetzturm-1; return (Einschlag
The title of this thread suggests you are dealing with some function that is returning an array of
int
s, yet yourBasistrefferermittlung()
function is simply returning a singleint
. Is that intentional?"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Hi, i have to ask a question about some codelines, where i have a mistake, because the array of int always returns 1 for every instance and i spent a lot of hours and still cannot find the fail i have maid. I simply post the necessary codelines in hope that someone can put away the wood infront of my head. Inside a modal dialogue, these are the calls
for (int y=0;y<5;y++)
{
Schadensnummer[y]=KlendautKlendaut_Gegner[0].Basistrefferermittlung();
}The class "KlendautKlendaut_Gegner0" - function Basistrefferermittlung:
int CBasis::Basistrefferermittlung()
{
int Zufallstreffer;
Zufallstreffer=rand()%99+1;
int Trefferchance;
Trefferchance=rand()%99+1;
static int Einschlagsnummer;if (Trefferchance>80) { if (Zufallstreffer>95) { if (m\_Bodyguard>1) { Einschlagsnummer=20; m\_Bodyguard=m\_Bodyguard-1; return (Einschlagsnummer); } else if (m\_Bodyguard<1&&m\_Palast>1) { Einschlagsnummer=21; m\_Palast=m\_Palast-1; return (Einschlagsnummer); } } else if (Zufallstreffer>90&&Zufallstreffer<95) { if (m\_Fabrik>1) { Einschlagsnummer=16; m\_Fabrik=m\_Fabrik-1; return (Einschlagsnummer); } } else if (Zufallstreffer>85&&Zufallstreffer<90) { if (m\_Scanner>1) { Einschlagsnummer=17; m\_Scanner=m\_Scanner-1; return (Einschlagsnummer); } } else if (Zufallstreffer>80&&Zufallstreffer<85) { if (m\_Kraftwerk>1) { Einschlagsnummer=18; m\_Kraftwerk=m\_Kraftwerk-1; return (Einschlagsnummer); } } else if (Zufallstreffer>75&&Zufallstreffer<80) { if (m\_Zentrale>1) { Einschlagsnummer=19; m\_Zentrale=m\_Zentrale-1; return (Einschlagsnummer); } } } if (m\_Schutzwall>1) { Einschlagsnummer=1; m\_Schutzwall=m\_Schutzwall-1; return (Einschlagsnummer); } else if (m\_Gefechtsstand>1) { Einschlagsnummer=2; m\_Gefechtsstand=m\_Gefechtsstand-1; return (Einschlagsnummer); } else if (m\_Geschuetzturm>1) { Einschlagsnummer=3; m\_Geschuetzturm=m\_Geschuetzturm-1; return (Einschlag
Some issues:
static int Einschlagsnummer;
Is never default initialized. Some code paths return nothing so whatever is in [e]ax gets returned, which may be 1. If Trefferchance is <= 80 and m_Schutzwall > 5, it will return 1 for each iteration up to five. However, you stated that m_Schutzwall is 1 which means it's never decremented to zero since the test is for m_Schutzwall > 1, not m_Schutzwall >= 1. This suggests that the return value is likely what happens to be in [e]ax. now nitpicking:
int Zufallstreffer;
Zufallstreffer=rand()%99+1;
int Trefferchance;
Trefferchance=rand()%99+1;Why not initialize the variables in the declaration of them? Why
m_Schutzwall=m_Schutzwall-1;
instead of --m_Schutzwall;