This site is a testing version, but all data is shared with the live forum.


Raised This Month: $ Target: $400
 0% 

Variable already declared?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
PreDominance
Member
Join Date: Jul 2014
Old 08-08-2014 , 10:16   Variable already declared?
Reply With Quote #1

Greetings,
I have an odd case where I have a chain of else-if's, and a variable declared in one elif is in the same scope as another. Am I not fully understanding scope, or is it just not a good idea to program at 8am?

Code:
....     else if (strfind(name, "numberoflevels") != -1) {         if (consistent) {             for (i = 0; i < RACE_MAXSKILLS; i++) RACE_SKILL_LEVELS[num][i] = str_to_num(value);         } else {             new arrIds[RACE_MAXSKILLS][3];             util_explodeString(arrIds, RACE_MAXSKILLS, 2, value, '|');             for (i = 0; i < RACE_MAXSKILLS; i++) RACE_SKILL_LEVELS[num][i] = str_to_num(arrIds[i]);         }     } else if (strfind(name, "skillnames") != -1) {         new arrIds[RACE_MAXSKILLS][20]; //Is fine????         util_explodeString(arrIds, RACE_MAXSKILLS, 19, value, '|');         for (i = 0; i < RACE_MAXSKILLS; i++) RACE_SKILL_NAMES[num][i] = arrIds[i];     } else if (strfind(name, "skilldesc") != -1) {         new arrIds[RACE_MAXSKILLS][50]; // LINE 79         util_explodeString(arrIds, RACE_MAXSKILLS, 49, value, '|');         for (i = 0; i < RACE_MAXSKILLS; i++) RACE_SKILL_INFO[num][i] = arrIds[i];     } else if (strfind(name, "skillreqlevel") != -1) {         new arrIds[RACE_MAXSKILLS][3]; //LINE 83         util_explodeString(arrIds, RACE_MAXSKILLS, 2, value, '|');         for (i = 0; i < RACE_MAXSKILLS; i++) RACE_SKILL_REQ_LEVEL[num][i] = str_to_num(arrIds[i]);     } ...

Last edited by PreDominance; 08-08-2014 at 10:16.
PreDominance is offline
Backstabnoob
BANNED
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 08-08-2014 , 11:57   Re: Variable already declared?
Reply With Quote #2

You have it defined somewhere else prior to the else-if block. Show the full function.

As a general 'lesson', whenever you're unsure whether there's a bug in the compiler or not, it's good to run a simple code through it:
PHP Code:
public x()
{
    new 
yz
    
if(z) { new var; }
    else if(
y) { new var; }

Compiles fine.

Last edited by Backstabnoob; 08-08-2014 at 12:00.
Backstabnoob is offline
PreDominance
Member
Join Date: Jul 2014
Old 08-08-2014 , 12:02   Re: Variable already declared?
Reply With Quote #3

Here's the full function.
PHP Code:
transferToArray(name[], value[], numbool:consistent)
{
    static 
i;
    if (
strfind(name"name") != -1copy(RACE_NAMES[num], 19value);
    else if (
strfind(name"author") != -1)            copy(RACE_AUTHORS[num], 19value);
    else if (
strfind(name"required_level") != -1RACE_REQ_LEVEL[num] = str_to_num(value);
    else if (
strfind(name"maximum_level") != -1)    RACE_MAX_LEVEL[num] = str_to_num(value);
    else if (
strfind(name"teamlimit") != -1)         RACE_TEAM_LIMIT[num] = str_to_num(value);
    else if (
strfind(name"numberofskills") != -1RACE_NUM_SKILLS[num] = str_to_num(value);
    else if (
strfind(name"allow_only") != -1) {
        new 
arrIds[RACE_MAX_PRIVATE_ID][35], i;
        
util_explodeString(arrIdsRACE_MAX_PRIVATE_ID34value'|');
        for (
0RACE_MAX_PRIVATE_IDi++) RACE_ALLOW_ONLY[num][i] = arrIds[i];}
    else if (
strfind(name"numberoflevels") != -1) {
        if (
consistent) {
            for (
0RACE_MAXSKILLSi++) RACE_SKILL_LEVELS[num][i] = str_to_num(value);
        } else {
            new 
arrIds[RACE_MAXSKILLS][3];
            
util_explodeString(arrIdsRACE_MAXSKILLS2value'|');
            for (
0RACE_MAXSKILLSi++) RACE_SKILL_LEVELS[num][i] = str_to_num(arrIds[i]);
        }
    } else if (
strfind(name"skillnames") != -1) {
        new 
arrIds[RACE_MAXSKILLS][20]; //<-- Why is this ok?
        
util_explodeString(arrIdsRACE_MAXSKILLS19value'|');
        for (
0RACE_MAXSKILLSi++) RACE_SKILL_NAMES[num][i] = arrIds[i];
    } else if (
strfind(name"skilldesc") != -1) {
        new 
arrIds[RACE_MAXSKILLS][50]; //<-- Line 79, error.
        
util_explodeString(arrIdsRACE_MAXSKILLS49value'|');
        for (
0RACE_MAXSKILLSi++) RACE_SKILL_INFO[num][i] = arrIds[i];
    } else if (
strfind(name"skillreqlevel") != -1) {
        new 
arrIds[RACE_MAXSKILLS][3];
        
util_explodeString(arrIdsRACE_MAXSKILLS2value'|');
        for (
0RACE_MAXSKILLSi++) RACE_SKILL_REQ_LEVEL[num][i] = str_to_num(arrIds[i]);
    }

My thought process is that if I'd already declared arrIds[] elsewhere in the code, the line right above 79 wouldn't work right.

*Edit
Bad indenting on here. Picture might be easier to read.

Last edited by PreDominance; 08-08-2014 at 12:16.
PreDominance is offline
Backstabnoob
BANNED
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 08-08-2014 , 12:49   Re: Variable already declared?
Reply With Quote #4

... Interesting.
PHP Code:
public func(param)
{
    if(
param
    {
        new var[
1][1]
    }
    else
    {
        new var[
1][1]
    } 

Won't compile (symbol already defined). If, however, the array is one dimensional, it works fine:

PHP Code:
public func(param)
{
    if(
param
    {
        new var[
1]
    }
    else
    {
        new var[
1]
    } 

This is definitely a compiler bug. I find it hard to believe that it wasn't discovered yet, though. Did you try compiling it with an earlier build?

Last edited by Backstabnoob; 08-08-2014 at 12:54.
Backstabnoob is offline
PreDominance
Member
Join Date: Jul 2014
Old 08-08-2014 , 13:05   Re: Variable already declared?
Reply With Quote #5

Uhh no, I have not. It's not a big deal to use different variable names though. Good to know I'm not crazy ;)

I assume you've reported the bug then, if not I can.
PreDominance is offline
Backstabnoob
BANNED
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 08-08-2014 , 13:06   Re: Variable already declared?
Reply With Quote #6

I'm a bit busy right now, so it'd be great if you reported the bug, but I can report it later myself.
Backstabnoob is offline
PreDominance
Member
Join Date: Jul 2014
Old 08-08-2014 , 13:07   Re: Variable already declared?
Reply With Quote #7

Taken care of. Thanks for the help.
PreDominance is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 07:11.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode