5

I don't know the cause of these errors I am receiving from Visual Studio 2010.

This is the code from my program from line 343 to line 408:

int create_den_from_img(char *img_file_name_part, int xlen, int ylen, int zlen )
{
  IplImage* imgs = 0;
  char str[80];
  unsigned char *data,*imgdata;

  /* allocating memory */
  data = (unsigned char *) malloc(xlen * ylen * zlen * sizeof(unsigned char) );
  if(data==NULL)
  {
    printf("error in allocating memory \n");
    exit(1);
  }

  /* Getting the filename & iterating through tiff images */

    for(int k = 0; k < zlen; k++)
    {   
        int count=2;
        int tmp=k+1;
        while(tmp/10)
        {
            count=count-1;
            tmp=tmp/10;
        }

        switch(count)
        {
            case 2:sprintf(str,"%s00%d.tif",img_file_name_part,k+1);
                    break;
            case 1:sprintf(str,"%s0%d.tif",img_file_name_part,k+1);
                    break;  
            default:sprintf(str,"%s%d.tif",img_file_name_part,k+1);
                    break;
        }
        printf("%s\n",str);

        /* Loading Image using OpenCV */
        imgs=cvLoadImage(str,-1);
        if(imgs==NULL)
        {
            printf("error in opening image \n");
            exit(1);
        }
        imgdata=(uchar *)imgs->imageData;

        for(int j =0; j < ylen; j++)
        {
            for(int i =0; i < xlen; i++)
            {
                data[ k*xlen*ylen + j*xlen + i ] = imgdata[ j*xlen+i ];
            }
        }

        cvReleaseImage(&imgs );
    }

    /* populating `data` variable is done. So, calling `write_den` */
    if(write_den("test.den",data,xlen,ylen,zlen)==0)
    {
        printf("Error in creating den file\n");
        exit(1);
    }
    printf("Den file created\n");

}

These are the list of errors:

Error   3   error C2143: syntax error : missing ';' before 'type'   c:\examples\denfile.c   358 1   MTP_TEST
Error   4   error C2143: syntax error : missing ';' before 'type'   c:\examples\denfile.c   358 1   MTP_TEST
Error   5   error C2143: syntax error : missing ')' before 'type'   c:\examples\denfile.c   358 1   MTP_TEST
Error   6   error C2143: syntax error : missing ';' before 'type'   c:\examples\denfile.c   358 1   MTP_TEST
Error   7   error C2065: 'k' : undeclared identifier    c:\examples\denfile.c   358 1   MTP_TEST
Error   9   error C2065: 'k' : undeclared identifier    c:\examples\denfile.c   358 1   MTP_TEST
Error   10  error C2059: syntax error : ')' c:\examples\denfile.c   358 1   MTP_TEST
Error   11  error C2143: syntax error : missing ';' before '{'  c:\examples\denfile.c   359 1   MTP_TEST
Error   12  error C2065: 'k' : undeclared identifier    c:\examples\denfile.c   361 1   MTP_TEST
Error   13  error C2065: 'k' : undeclared identifier    c:\examples\denfile.c   370 1   MTP_TEST
Error   14  error C2065: 'k' : undeclared identifier    c:\examples\denfile.c   372 1   MTP_TEST
Error   15  error C2065: 'k' : undeclared identifier    c:\examples\denfile.c   374 1   MTP_TEST
Error   16  error C2143: syntax error : missing ';' before 'type'   c:\examples\denfile.c   388 1   MTP_TEST
Error   17  error C2143: syntax error : missing ';' before 'type'   c:\examples\denfile.c   388 1   MTP_TEST
Error   18  error C2143: syntax error : missing ')' before 'type'   c:\examples\denfile.c   388 1   MTP_TEST
Error   19  error C2143: syntax error : missing ';' before 'type'   c:\examples\denfile.c   388 1   MTP_TEST
Error   20  error C2065: 'j' : undeclared identifier    c:\examples\denfile.c   388 1   MTP_TEST
Error   22  error C2065: 'j' : undeclared identifier    c:\examples\denfile.c   388 1   MTP_TEST
Error   23  error C2059: syntax error : ')' c:\examples\denfile.c   388 1   MTP_TEST
Error   24  error C2143: syntax error : missing ';' before '{'  c:\examples\denfile.c   389 1   MTP_TEST
Error   25  error C2143: syntax error : missing ';' before 'type'   c:\examples\denfile.c   390 1   MTP_TEST
Error   26  error C2143: syntax error : missing ';' before 'type'   c:\examples\denfile.c   390 1   MTP_TEST
Error   27  error C2143: syntax error : missing ')' before 'type'   c:\examples\denfile.c   390 1   MTP_TEST
Error   28  error C2143: syntax error : missing ';' before 'type'   c:\examples\denfile.c   390 1   MTP_TEST
Error   29  error C2065: 'i' : undeclared identifier    c:\examples\denfile.c   390 1   MTP_TEST
Error   31  error C2065: 'i' : undeclared identifier    c:\examples\denfile.c   390 1   MTP_TEST
Error   32  error C2059: syntax error : ')' c:\examples\denfile.c   390 1   MTP_TEST
Error   33  error C2143: syntax error : missing ';' before '{'  c:\examples\denfile.c   391 1   MTP_TEST
Error   34  error C2065: 'k' : undeclared identifier    c:\examples\denfile.c   392 1   MTP_TEST
Error   35  error C2065: 'j' : undeclared identifier    c:\examples\denfile.c   392 1   MTP_TEST
Error   36  error C2065: 'i' : undeclared identifier    c:\examples\denfile.c   392 1   MTP_TEST
Error   37  error C2065: 'j' : undeclared identifier    c:\examples\denfile.c   392 1   MTP_TEST
Error   38  error C2065: 'i' : undeclared identifier    c:\examples\denfile.c   392 1   MTP_TEST

I've been getting these kind of errors all day long. Sometimes the code compiles, while at other time it doesn't. Its really annoying.

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
volpack
  • 835
  • 1
  • 8
  • 7
  • did you copy/past some code from another editor/web browser? I've had problems like this before when there were hidden characters present in the pasted code. – Eric Schweichler Apr 24 '10 at 22:53
  • 2
    No offense, but I don't think the problem lies with MSVC. – Billy ONeal Apr 24 '10 at 22:53
  • What command line flags are you passing to the compiler? – mmmmmm Apr 24 '10 at 22:55
  • 1
    @Eric Schweichler : No, I didn't copy paste this code. Its completely hand written. @Billy ONeal: I too think so. But I'm unable to find error by myself. – volpack Apr 24 '10 at 22:56
  • That might be true, but your original question looked like you were bashing MSVC. The original question might have been closed as subjective and argumentative. I have edited it and hopefully it will be better received now. Please revert it if you do not like the edits. – Billy ONeal Apr 24 '10 at 23:00
  • 1
    Personally, if the compiler can't output helpful error messages, I count it to be a problem with the compiler. And if you have to say things like, "the error message was helpful--anyone who has 5 years of daily Visual C++ experience can interpret it!" (or some such) to defend the compiler, you're only proving my point. – weberc2 Oct 22 '12 at 21:42

2 Answers2

13

You're compiling a .c file, which for Microsoft Visual Studio means that you need to be writing C89 (aka C90) code, not C99 code or C++.

What this means is that you must declare your variables at the start of each block. This means that you can't do:

for (int k = 0; ...

You have to declare k at the start of the block and do:

for (k = 0; ...
CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • Or just compile it as C++ code. It's all valid C++ as well. +1. – Billy ONeal Apr 24 '10 at 23:05
  • @Billy: Where do I have options to set these various languages? – volpack Apr 24 '10 at 23:12
  • @Charles Bailey: Where do I find options to set if it should use c89/c99/c++ ? – volpack Apr 24 '10 at 23:15
  • @Billy: If it's meant to be a C++ source file I'd have thought that it would be simpler to just give it an extension that MSVC recognizes as C++ out of the box, but yes `/TP` is one way. – CB Bailey Apr 24 '10 at 23:17
  • @volpack: I'm not sure, but you could just rename the file with a `.cpp` extension instead of `.c`. – Billy ONeal Apr 24 '10 at 23:18
  • @volpack: MSVC doesn't support C99, only C89 + extensions. If you want to write C++ the simplest thing to do is to give the file an appropriate extension (.cpp or .cc). There's also the `/TP` compiler option to force the compiler to treat any source file as a C++ source file. I think that you can control it in the IDE in the property pages for the file IIRC. – CB Bailey Apr 24 '10 at 23:26
  • 1
    What?? MSVC doesn't support C99? How come? Its been more than 10 years. Why doesn't it support? AFAIK, compilers compete in the race of "who adds new features first?" – volpack Apr 24 '10 at 23:39
  • 1
    MS aren't running in the C race. http://stackoverflow.com/questions/146381/visual-studio-support-for-new-c-c-standards – CB Bailey Apr 24 '10 at 23:51
2

Do you have a missing semicolon after a class/struct declaration before line 343 ?

One thing you can do is try another compiler to see if you get a different error message that speaks more to you. For example, there's Comeau online.

Otherwise, could you have some rogue #defines in your code or some of your includes? Since this doesn't happen all the time, could someone be playing tricks on you? Try to examine the preprocessed output.

JRL
  • 76,767
  • 18
  • 98
  • 146
  • Nope. checked thoroughly. I even tried deleting just this code & compiling. It compiles fine. – volpack Apr 24 '10 at 23:00
  • @JRL: The first error is at the first for loop inside this function -- I think if it was a missing class ending semicolon there'd be an error when int was encountered at the start of the function definition. But good psychic debugging -- this is the first thing I'd check. +1. – Billy ONeal Apr 24 '10 at 23:01