Raised This Month: $41 Target: $400
 10% 

Possibly erroneous warning when declaring enum structs with primitive members


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Sappykun
Junior Member
Join Date: Nov 2019
Old 03-03-2021 , 23:03   Possibly erroneous warning when declaring enum structs with primitive members
Reply With Quote #1

I have the following snippet of code:

PHP Code:
enum struct Survivor1 {
  
char name[MAX_NAME_LENGTH];
  
char model[PLATFORM_MAX_PATH];
  
int prop;
  
char adminflags[16];
}

enum struct Survivor2 {
  
char name[MAX_NAME_LENGTH];
  
char model[PLATFORM_MAX_PATH];
  
int prop[1];
  
char adminflags[16];
}

public 
void OnPluginStart()  
{
    
Survivor1 e = { "Ellis""models/survivors/survivor_mechanic.mdl"3"bcd" }; // line 17
    
Survivor2 z = { "Zoey""models/survivors/survivor_teenangst.mdl"5"z" };
    
    
PrintToServer("%s %s %i %s"e.namee.modele.prope.adminflags);
    
PrintToServer("%s %s %i %s"z.namez.modelz.propz.adminflags); // line 21

Compiling this code produces the following warning:

test.sp(17) : warning 228: length of initializer exceeds size of the enum field

but otherwise compiles perfectly fine.

I am confused by this, especially since activating the plugin doesn't throw any errors:

Code:
sm plugins load custom/optional/test
Ellis models/survivors/survivor_mechanic.mdl 3 bcd
Zoey models/survivors/survivor_teenangst.mdl 5 z
[SM] Loaded plugin custom/optional/test.smx successfully.
If I change the PrintToServer lines to the following:
PHP Code:
PrintToServer("%s %s %i %s"e.namee.modele.prop 4e.adminflags);
PrintToServer("%s %s %i %s"z.namez.modelz.prop 4z.adminflags); 
and attempt to recompile, I get this error:

test.sp(21) : error 033: array must be indexed (variable "prop")

Why do I need to declare prop as an array to make the warning go away? IntToString (which is used in my plugin to apply the prop's value to a player) doesn't accept integer arrays, and I can't perform operations on z.prop without referencing index 0 constantly.

I have a feeling that my code (Survivor1 specifically) is fine, and the compiler is throwing a false warning. Can someone with more SourcePawn experience confirm or deny my suspicions?

Last edited by Sappykun; 03-07-2021 at 00:44.
Sappykun is offline
Effeff
AlliedModders Donor
Join Date: Jan 2019
Location: discord: ff#0533
Old 03-04-2021 , 03:29   Re: Cannot declare enum structs that mix primitive members and array members
Reply With Quote #2

Example of how you can initialize the enum struct without the warning (compiles but untested).

PHP Code:
enum struct Survivor1
{
    
char name[MAX_NAME_LENGTH];
    
char model[PLATFORM_MAX_PATH];
    
int prop;
    
char adminflags[16];
    
void Create(char name[MAX_NAME_LENGTH], char model[PLATFORM_MAX_PATH], int propchar adminflags[16])
    {
        
strcopy(this.nameMAX_NAME_LENGTHname);
        
strcopy(this.modelPLATFORM_MAX_PATHmodel);
        
this.prop prop;
        
strcopy(this.adminflags16adminflags);
    }
}

public 
void OnPluginStart()  
{
    
Survivor1 e;
    
e.Create("Ellis""models/survivors/survivor_mechanic.mdl"3"bcd");
    
    
PrintToServer("%s %s %i %s"e.namee.modele.prope.adminflags);


Last edited by Effeff; 03-04-2021 at 03:31.
Effeff is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 03-04-2021 , 08:55   Re: Cannot declare enum structs that mix primitive members and array members
Reply With Quote #3

What compiler version are you using? enum structs didn't support initializer lists until 1.11.
__________________
asherkin is offline
Sappykun
Junior Member
Join Date: Nov 2019
Old 03-04-2021 , 12:21   Re: Cannot declare enum structs that mix primitive members and array members
Reply With Quote #4

Quote:
Originally Posted by asherkin View Post
What compiler version are you using? enum structs didn't support initializer lists until 1.11.
1.10.0.6502, latest stable version at the time of writing.
Sappykun is offline
Reply


Thread Tools
Display Modes

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 09:24.


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