Проблемы с компилятором на VC ++ 2008 Express, казалось бы, правильный код выдает ошибки - PullRequest
2 голосов
/ 10 апреля 2010

Я некоторое время пытался вернуться к кодированию, поэтому я решил, что начну с некоторого простого SDL, теперь, без файлового ввода-вывода, это прекрасно компилируется, но когда я добавляю код 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 ]

3 голосов
/ 10 апреля 2010
#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 голос
/ 10 апреля 2010
  1. Вам не нужно объявлять, открывать или закрывать stdin, stdout или stderr, это уже сделано для вас в stdio.
  2. Если вы используете C ++, почему бы не iostream?
...