试图将头文件包含到main.cpp中,但是VS2010给了我错误

时间:2013-03-11 01:34:18

标签: c++ visual-studio-2010 header-files

我试图在我的主cpp中使用一个简单的头文件,但我的编译器一直在生成错误。如果我想要在头文件中的代码在头文件中,它会产生错误;如果头文件中没有代码,并且我想要在头文件中的代码在主cpp中,编译器将不会生成错误。我已经检查过以确保头文件与我的主文件位于同一位置,是的,因为我使用Allegro制作游戏,所以我确保链接调试库;所有这些都得到了照顾。

这是我的代码:

main.cpp(没有代码我希望在头文件中):

#include <iostream>
#include <allegro5\allegro5.h>
#include <allegro5\allegro_primitives.h>
#include <fstream>
#include "Globals.h"
using namespace std;

int main()
{
    ALLEGRO_DISPLAY *display = NULL;
    ALLEGRO_EVENT_QUEUE *event_queue = NULL;
    ALLEGRO_TIMER *timer = NULL;

    if(!al_init())  
    {
        if(errorFile.is_open())
            errorFile << "Allegro failed to initialize.";
        return -1;
    }

    display = al_create_display(WINDOW_WIDTH, WINDOW_HEIGHT);           

    if(!display)    
    {
        if(errorFile.is_open())
            errorFile << "ALLEGRO_DISPLAY failed to initialize.";
        return -1;
    }

    if(errorFile.bad())
    {
        cout << "errorFile is bad";
    }

    al_init_primitives_addon();
    al_install_keyboard();

    event_queue = al_create_event_queue();
    timer = al_create_timer(1.0 / 60);

    al_register_event_source(event_queue, al_get_keyboard_event_source());
    al_register_event_source(event_queue, al_get_timer_event_source(timer));
    al_register_event_source(event_queue, al_get_display_event_source(display));

    al_start_timer(timer);

    while(!done)
    {
        ALLEGRO_EVENT ev;
        al_wait_for_event(event_queue, &ev);
        if(ev.type == ALLEGRO_EVENT_TIMER)
        {
            if(ev.timer.source == timer)
            {

            }
        }
        if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
        {
            done = true;
        }
        if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
        {
            switch(ev.keyboard.keycode)
            {
                case ALLEGRO_KEY_ESCAPE:
                    done = true;
                    break;
            }
        }
        if(ev.type == ALLEGRO_EVENT_KEY_UP)
        {
            switch(ev.keyboard.keycode)
            {
                case ALLEGRO_KEY_ESCAPE:
                    done = true;
                    break;
            }
        }
        if(redraw && al_is_event_queue_empty(event_queue))
        {
            al_flip_display();
            al_clear_to_color(al_map_rgb(0,0,0));
        }
    }
    al_destroy_display(display);
    al_destroy_event_queue(event_queue);
    al_destroy_timer(timer);
    errorFile.close();
    return 0;
}

这里是头文件“Globals.h”,带有所需的代码:

#include <iostream>
#include <fstream>

const int WINDOW_WIDTH = 600;
const int WINDOW_HEIGHT = 600;
bool done = true;
bool redraw = true;

enum KEYS {UP,DOWN,LEFT,RIGHT};
bool keys[4] = {false,false,false,false};

ofstream errorFile ("errorFile.txt", ios::trunc);

提前致谢!

1 个答案:

答案 0 :(得分:0)

尝试在#include allegro标题周围添加“”,这告诉编译器首先查看项目目录。虽然您可以将错误日志放在帖子中。