-
March 3rd, 2009, 09:18 AM
#1
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
-
March 3rd, 2009, 09:22 AM
#2
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
-
March 3rd, 2009, 10:32 AM
#3
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)
-
March 3rd, 2009, 10:50 AM
#4
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
-
March 3rd, 2009, 06:00 PM
#5
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.
-
March 3rd, 2009, 08:30 PM
#6
Re: array of structs
What do you mean? That should work fine.
-
March 3rd, 2009, 10:07 PM
#7
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?
-
March 3rd, 2009, 10:19 PM
#8
Re: array of structs
Try dropping the word "struct" from that as was suggested above.
-
March 3rd, 2009, 11:06 PM
#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];
-
March 4th, 2009, 12:21 AM
#10
Re: array of structs
Try this one.
Code:
typedef struct vertex2 {
...
} Vertex2;
Vertex2 localBuffer[264];
-
March 4th, 2009, 04:30 AM
#11
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
-
March 4th, 2009, 04:52 AM
#12
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,,,
-
March 4th, 2009, 08:31 AM
#13
Re: array of structs
Originally Posted by mikazo
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?
-
March 4th, 2009, 09:40 AM
#14
Re: array of structs
mikazo, just post a SSCCE
-
March 4th, 2009, 09:44 AM
#15
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|