CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Feb 2009
    Posts
    9

    Exclamation array of structs

    I have the following code:

    Code:
    struct vertex2 {
    	FLOAT x, y, z, rhw;
    	DWORD color;
    	FLOAT u, v;
    };
    
    struct vertex2 localBuffer[264];
    localBuffer[0].x = 512.0f;
    But I get the following compiler error:

    Code:
    1>------ Rebuild All started: Project: Erg, Configuration: Debug Win32 ------
    1>Deleting intermediate and output files for project 'Erg', configuration 'Debug|Win32'
    1>Compiling...
    1>main.cpp
    1>c:\users\mike\documents\visual studio 2008\projects\erg\erg\localbuffer.h(5) : error C2466: cannot allocate an array of constant size 0
    1>c:\users\mike\documents\visual studio 2008\projects\erg\erg\localbuffer.h(5) : error C2143: syntax error : missing ';' before '.'
    1>c:\users\mike\documents\visual studio 2008\projects\erg\erg\localbuffer.h(5) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\users\mike\documents\visual studio 2008\projects\erg\erg\localbuffer.h(5) : error C2371: 'localBuffer' : redefinition; different basic types
    1>        c:\users\mike\documents\visual studio 2008\projects\erg\erg\localbuffer.h(2) : see declaration of 'localBuffer'
    1>Build log was saved at "file://c:\Users\Mike\Documents\Visual Studio 2008\Projects\Erg\Erg\Debug\BuildLog.htm"
    1>Erg - 4 error(s), 0 warning(s)
    ========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
    As far as I know it should work how it is.. can anyone please explain to me? Thanks.

    -Mike

  2. #2
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: array of structs

    You cannot make assignments on the top level, only declarations.

    However, it is possible to work around this limitation as follows:

    Code:
    float mydummyvariable1 = localBuffer[0].x = 512.0f;
    float mydummyvariable2 = localBuffer[0].y = 1024.0f;
    float mydummyvariable3 = localBuffer[0].z = 2048.0f;
    Nobody cares how it works as long as it works

  3. #3
    Join Date
    Feb 2005
    Posts
    2,160

    Re: array of structs

    For C++, lose the "struct" part of your array declaration:

    Code:
    struct vertex2 {
    	FLOAT x, y, z, rhw;
    	DWORD color;
    	FLOAT u, v;
    };
    
    vertex2 localBuffer[264];
    localBuffer[0].x = 512.0f;
    For C, tag your declaration then lose the "struct" as so:
    Code:
    struct tag_vertex2 {
    	FLOAT x, y, z, rhw;
    	DWORD color;
    	FLOAT u, v;
    }vertex2;
    
    vertex2 localBuffer[264];
    localBuffer[0].x = 512.0f;
    (not tested)

  4. #4
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: array of structs

    With plain C you can also do:
    Code:
    typedef struct vertex2 vertex2;
    Nobody cares how it works as long as it works

  5. #5
    Join Date
    Feb 2009
    Posts
    9

    Re: array of structs

    Thank you for the workaround, but I guess I should have mentioned that I'd prefer to execute the code as follows:

    Code:
    for (int i = 0; i < 100; i++)
          localBuffer[i].x = 10.0f;
    The problem with that is that I can't really declare new dummy floats inside the loop. It seems strange to me that Visual C++ would disallow such a thing.

  6. #6
    Join Date
    Feb 2005
    Posts
    2,160

    Re: array of structs

    What do you mean? That should work fine.

  7. #7
    Join Date
    Feb 2009
    Posts
    9

    Re: array of structs

    Ok, now this is just weird.

    This code

    Code:
    struct vertex2 localBuffer[264];
    for (int i = 4; i < 264; i++) {
    		localBuffer[i].x = 0.0f;
    produces this error

    Code:
    1>c:\users\mike\documents\visual studio 2008\projects\erg\erg\localbuffer.h(38) : error C2059: syntax error : 'for'
    1>c:\users\mike\documents\visual studio 2008\projects\erg\erg\localbuffer.h(38) : error C2143: syntax error : missing ')' before ';'
    1>c:\users\mike\documents\visual studio 2008\projects\erg\erg\localbuffer.h(38) : error C2143: syntax error : missing ';' before '<'
    1>c:\users\mike\documents\visual studio 2008\projects\erg\erg\localbuffer.h(38) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\users\mike\documents\visual studio 2008\projects\erg\erg\localbuffer.h(38) : error C2143: syntax error : missing ';' before '++'
    1>c:\users\mike\documents\visual studio 2008\projects\erg\erg\localbuffer.h(38) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\users\mike\documents\visual studio 2008\projects\erg\erg\localbuffer.h(38) : error C2086: 'int i' : redefinition
    1>        c:\users\mike\documents\visual studio 2008\projects\erg\erg\localbuffer.h(38) : see declaration of 'i'
    1>c:\users\mike\documents\visual studio 2008\projects\erg\erg\localbuffer.h(38) : error C2059: syntax error : ')'
    1>c:\users\mike\documents\visual studio 2008\projects\erg\erg\localbuffer.h(38) : error C2143: syntax error : missing ';' before '{'
    1>c:\users\mike\documents\visual studio 2008\projects\erg\erg\localbuffer.h(38) : error C2447: '{' : missing function header (old-style formal list?)
    Syntax error 'for'? There's no such thing as a for loop anymore?

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: array of structs

    Try dropping the word "struct" from that as was suggested above.

  9. #9
    Join Date
    Feb 2009
    Posts
    9

    Re: array of structs

    I've tried it with all of the following and still get errors:

    Code:
    struct vertex2 localBuffer[264];
    
    vertex2 localBuffer[264];
    
    struct vertex2 {
    ...
    } Vertex2;
    Vertex2 localBuffer[264];

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: array of structs

    Try this one.

    Code:
    typedef struct vertex2 {
    ...
    } Vertex2;
    Vertex2 localBuffer[264];

  11. #11
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: array of structs

    You cannot write code on the top level. Your program needs a main function.

    Code:
    int main(int argc, char *argv[]) {
      for (int i = 0; i < 100; i++)
        localBuffer[i].x = 10.0f;
    }
    Nobody cares how it works as long as it works

  12. #12
    Join Date
    Aug 2008
    Posts
    112

    Re: array of structs

    Yes, but the OP programs in win32 application not console
    Why doesn't OP use vector class ?

    std::vector<vertex2>localBuffer;
    struct or class is fine, no problem really

    [Today I suddenly am in a better mood! Thanks for reading and sharing!]

    Regards

    Stupid ~V
    hi,,,

  13. #13
    Join Date
    Feb 2005
    Posts
    2,160

    Re: array of structs

    Quote Originally Posted by mikazo View Post
    Ok, now this is just weird.

    This code

    Code:
    struct vertex2 localBuffer[264];
    for (int i = 4; i < 264; i++) {
    		localBuffer[i].x = 0.0f;
    produces this error

    Code:
    1>c:\users\mike\documents\visual studio 2008\projects\erg\erg\localbuffer.h(38) : error C2059: syntax error : 'for'
    1>c:\users\mike\documents\visual studio 2008\projects\erg\erg\localbuffer.h(38) : error C2143: syntax error : missing ')' before ';'
    1>c:\users\mike\documents\visual studio 2008\projects\erg\erg\localbuffer.h(38) : error C2143: syntax error : missing ';' before '<'
    1>c:\users\mike\documents\visual studio 2008\projects\erg\erg\localbuffer.h(38) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\users\mike\documents\visual studio 2008\projects\erg\erg\localbuffer.h(38) : error C2143: syntax error : missing ';' before '++'
    1>c:\users\mike\documents\visual studio 2008\projects\erg\erg\localbuffer.h(38) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\users\mike\documents\visual studio 2008\projects\erg\erg\localbuffer.h(38) : error C2086: 'int i' : redefinition
    1>        c:\users\mike\documents\visual studio 2008\projects\erg\erg\localbuffer.h(38) : see declaration of 'i'
    1>c:\users\mike\documents\visual studio 2008\projects\erg\erg\localbuffer.h(38) : error C2059: syntax error : ')'
    1>c:\users\mike\documents\visual studio 2008\projects\erg\erg\localbuffer.h(38) : error C2143: syntax error : missing ';' before '{'
    1>c:\users\mike\documents\visual studio 2008\projects\erg\erg\localbuffer.h(38) : error C2447: '{' : missing function header (old-style formal list?)
    Syntax error 'for'? There's no such thing as a for loop anymore?
    Are you implementing this in your header file?

  14. #14
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: array of structs

    mikazo, just post a SSCCE

  15. #15
    Join Date
    Feb 2009
    Posts
    9

    Re: array of structs

    Ok I feel silly now. I've been initializing my buffer arrays in header files because they can get large and tedious, but when I went to initialize my localBuffer after it was declared, I totally forgot that you can't just execute code outside of main().

    Thank you for spending time to help fix my stupid mistake.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured