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

时间:2010-04-10 00:48:02

标签: c++ windows file-io sdl stdio

我一直试图重新编写一段时间,所以我想我会从一些简单的SDL开始,现在,没有文件i / o,编译很好,但是当我输入stdio代码时,它开始抛出错误。这个我不确定,我没有看到代码本身的任何问题,但是,就像我说的,我可能也是一个新手,并且认为我会来这里找一个有更多经验的人这种类型的东西要看它。

我想我的问题归结为:“为什么不在Microsoft的Visual C ++ 2008 Express下编译?”

我已将错误日志附加到代码段的底部。提前感谢您的帮助。

#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;
}

报告的实际错误:

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

2 个答案:

答案 0 :(得分:4)

#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;
}

尝试将名称stderr和stdout更改为其他名称。我怀疑编译器在抱怨,因为这些已经在C库的其他地方定义过。

答案 1 :(得分:1)

  1. 您无需声明,打开或关闭已在stdin为您完成的stdoutstderrstdio
  2. 如果您使用的是C ++,为什么不使用iostream