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

标签 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

最佳答案

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

关于c++ - VC++ 2008 Express 上的编译器问题,看似正确的代码会引发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2611803/

相关文章:

c++ - 函数不读取二维数组的 [i][0] 行

c++ - 在 C++ 中使用虚函数

c++ - c++中的线程,在二维数组中找到最大的条目

c++ - 设置静态成员指针变量

c++ - 有没有办法在 ID3DUserDefinedAnnotation 上调用 SetPrivateData?

Windows 查询 session 状态

c++ - 当文件名包含宽字符时使用 ifstream

linux - 在 SSD 上安装 Unix 套接字文件是否会提高性能?

file-io - Fortran 将文件读入数组 - 转置维度

c - 二进制文件读取,在c中添加额外的字符?