2

我一直在尝试重新编码,所以我想我会从一些简单的 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
4

2 回答 2

3
#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 库的其他地方定义了。

于 2010-04-10T00:51:51.977 回答
1
  1. 您不需要声明、打开或关闭stdinstdout或者stderr,这已经在stdio.
  2. 如果您使用的是 C++,为什么不iostream呢?
于 2010-04-10T00:57:10.407 回答