简体   繁体   English

typedef struct出错C2275错误

[英]error C2275 error with typedef struct

I have a program I am writing which is a basic image drawing program. 我有一个我正在编写的程序,它是一个基本的图像绘制程序。 It is in C. 它在C.

Initialy i declare 最初我宣布

typedef struct 
{   
  int red;
  int green;
  int blue;
} pixel_colour;

I have a function to fill the background which accepts this so I use it like. 我有一个函数来填充接受这个的背景,所以我用它就像。

pixel_colour flood_colour = {80,50,91};
FloodImage(flood_colour);

Now this works fine if it is the only thing in my main, but as soon as I add a switch/case and the rest of my code I can no longer use pixel_colour flood_colour = {80,50,91}; 现在这可以正常工作,如果它是我主要的唯一的东西,但只要我添加一个开关/案例和我的其余代码我不能再使用pixel_colour flood_colour = {80,50,91};

instead getting 反而得到

error C2275: 'pixel_colour' : illegal use of this type as an expression
1>          c:\users\xxxx\documents\visual studio 2010\projects\xxx.c(20) : see declaration of 'pixel_colour'

The main code is below, it works fine with all my functions until i try to use pixel_colour, It will be set to variable rather than 200,200,200 but even that does not work 主要代码如下,它可以正常使用我的所有功能,直到我尝试使用pixel_colour,它将被设置为变量而不是200,200,200,但即使这样也行不通

char instring[80] = "FL 201 3 56";
  int pst = FirstTwo(instring);
  switch( pst )
  {
  case 1: 
    printf( "FL ");
    CaseFL(instring);
    pixel_colour flood_colour = {200,200,200};
    FloodImage(flood_colour);
    break;

  case 2: 
    printf( "LI" );
    break;

  case 3: 
    printf( "RE" );
    break;

  case 4: 
    printf( "CH" );
    break;

  case 5: 
    printf( "FI" );    
    break;

  case 6: 
    printf( "EX" );    
    exit(EXIT_FAILURE);
    break;

  default  : 
    printf( "Something went wrong" );

    break;
  }

In C89, as supported by MSVC, you can declare a variable only at the beginning of a code block. 在C89中,由MSVC支持,您只能在代码块的开头声明一个变量。 Instead you can do: 相反,你可以这样做:

case 1: 
{
    // first thing in the block - variable declaration / initialization
    pixel_colour flood_colour = {200,200,200};
    printf( "FL ");
    CaseFL(instring);
    FloodImage(flood_colour);
    break;
}

C99, C11 and C++ all allow variables to be declared as needed. C99,C11和C ++都允许根据需要声明变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM