typedef struct { ... } S1

G

Guest

Hi there!

I have a very wired problem when I develop a C/C++ based application using
Visual Studio 2005.

#include <stdio.h>

typedef struct _S1 {
int data;
int text;
} S1;


int main()
{
printf("Hello, Vorbis!\n");
S1 oy;
}

Code above doen't compile. On line 12 it states error C2275: 'S1': illegal
use of this type as an expression.

This code compiles well in gcc. So I wonder if I have wrong setting
somewhere in Visual Studio.

Hope somebody can give me a hint.
 
G

Guest

Aha, it has been a while since I coded C last time. I have been spoiled by
C#, java.

In C declartion must be made in the beginning of the block. It works fine now.

/Wei
 
D

David Wilkinson

Wei said:
Aha, it has been a while since I coded C last time. I have been spoiled by
C#, java.

In C declartion must be made in the beginning of the block. It works fine now.

/Wei

Wei:

Unless you have a special reason to use C, I suggest you compile your
program as C++. The you can put declarations where you like. Just change
your file extension to .cpp or .cxx rather than .c. It is also possible
to change the compile setting for the .c files in your project, so that
they are compiled as C++ (but I think you have to do each file separately).

David Wilkinson
 
J

Jeffrey Tan[MSFT]

In additionally, this C/C++ compiler option can be specified in Project
Setting dialog->C/C++->Advanced->"Compile As".

Hope this helps

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

David Wilkinson

Jeffrey said:
In additionally, this C/C++ compiler option can be specified in Project
Setting dialog->C/C++->Advanced->"Compile As".

Jeffrey:

In my experience (in 2003.NET) this does not work, at least in my
situation. I had an existing C++ project, to which I wanted to add a
bunch. of .c files, and compile them as C++. I was not the owner of
these files, so I did not want to change the extension (which I would
probably have done otherwise). The setting you mention was already set
to C++, but the added files compiled as C, and it seemed I had to change
each one. In the new VStudio IDE this is very painful because you have
to open the Properties dialog separately for each file.

David Wilkinson
 
J

Jeffrey Tan[MSFT]

Hi David,

However, in my VS.net2003 project, if I added a test.c file in the project,
then add the following code to the file:

#include <stdio.h>

typedef struct _S1 {
int data;
int text;
} S1;


int test1()
{
printf("Hello, Vorbis!\n");
S1 oy;
return 1;
}

With that compiler option set to C++, it will compile without any problem.
It will only generate error when setting the compiler to C style.

Can you please give it a re-check? Thanks

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top