简体   繁体   English

VC ++ 2008 Express上的编译器问题,看似正确的代码引发错误

[英]Compiler issues on VC++ 2008 Express, Seemingly correct code throws errors

I've been trying to get back into coding for a while, so I figured I'd start with some simple SDL, now, without the file i/o, this compiles fine, but when I throw in the stdio code, it starts throwing errors. 我一直在尝试重新编码,因此我以为我会从一些简单的SDL开始,现在,没有文件I / O,这可以很好地编译,但是当我输入stdio代码时,它就开始了引发错误。 This I'm not sure about, I don't see any problem with the code itself, however, like I said, I might as well be a newbie, and figured I'd come here to get someone with a little more experience with this type of thing to look at it. 我不确定这一点,我认为代码本身没有任何问题,但是,就像我说的那样,我最好还是一个新手,并认为我会来这里找一些有更多经验的人这种东西要看一下。

I guess my question boils down to: "Why doesn't this compile under Microsoft's Visual C++ 2008 Express?" 我想我的问题可以归结为:“为什么不使用Microsoft的Visual C ++ 2008 Express进行编译?”

I've attached the error log at the bottom of the code snippet. 我在代码段的底部附加了错误日志。 Thanks in advance for any help. 在此先感谢您的帮助。

#include "SDL/SDL.h"
#include "stdio.h"

int main(int argc, char *argv[])
{
    FILE *stderr;
    FILE *stdout; 

    stderr = fopen("stderr", "wb");
    stdout = fopen("stdout", "wb");

    SDL_Init(SDL_INIT_EVERYTHING);
    fprintf(stdout, "SDL INITIALIZED SUCCESSFULLY\n");
    SDL_Quit();
    fprintf(stderr, "SDL QUIT.\n");

    fclose(stderr);
    fclose(stdout);

    return 0;
}

Actual errors reported: 报告的实际错误:

main.cpp(6) : error C2090: function returns array
main.cpp(6) : error C2528: '__iob_func' : pointer to reference is illegal
main.cpp(6) : error C2556: 'FILE ***__iob_func(void)' : overloaded function differs only by return type from 'FILE *__iob_func(void)'
       c:\program files\microsoft visual studio 9.0\vc\include\stdio.h(132) : see declaration of '__iob_func'
main.cpp(7) : error C2090: function returns array
main.cpp(7) : error C2528: '__iob_func' : pointer to reference is illegal
main.cpp(9) : error C2440: '=' : cannot convert from 'FILE *' to 'FILE ***'
       Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
main.cpp(10) : error C2440: '=' : cannot convert from 'FILE *' to 'FILE ***'
       Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
main.cpp(13) : error C2664: 'fprintf' : cannot convert parameter 1 from 'FILE ***' to 'FILE *'
       Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
main.cpp(15) : error C2664: 'fprintf' : cannot convert parameter 1 from 'FILE ***' to 'FILE *'
       Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
main.cpp(17) : error C2664: 'fclose' : cannot convert parameter 1 from 'FILE ***' to 'FILE *'
       Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
main.cpp(18) : error C2664: 'fclose' : cannot convert parameter 1 from 'FILE ***' to 'FILE *'
       Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
#include "SDL/SDL.h"
#include "stdio.h"

int main(int argc, char *argv[])
{
    FILE *stderr; //Invalid names. These are already defined by stdio.h.
    FILE *stdout; //You can't use them (portably anyway).

    stderr = fopen("stderr", "wb"); //I'm assuming you actually want files
    stdout = fopen("stdout", "wb"); //called "stderror" and "stdout".

    SDL_Init(SDL_INIT_EVERYTHING);
    fprintf(stdout, "SDL INITIALIZED SUCCESSFULLY\n");
    SDL_Quit();
    fprintf(stderr, "SDL QUIT.\n");

    fclose(stderr);
    fclose(stdout);

    return 0;
}

Try changing the names stderr and stdout to something else. 尝试将名称stderr和stdout更改为其他名称。 I suspect the compiler is complaining because these are already defined elsewhere in the C library. 我怀疑编译器在抱怨,因为这些已经在C库的其他地方定义了。

  1. You don't need to declare, open, or close stdin , stdout or stderr , that's already done for you in stdio . 您不需要声明,打开或关闭stdinstdoutstderr ,这已经在stdio为您完成了。
  2. If you're using C++, why not iostream ? 如果您使用的是C ++,为什么不使用iostream

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

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