【问题标题】:Trying to include a header file into main.cpp, but VS2010 gives me errors试图在 main.cpp 中包含一个头文件,但是 VS2010 给了我错误
【发布时间】:2013-02-26 02:49:09
【问题描述】:

我试图在我的主 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);

提前谢谢!

【问题讨论】:

  • 我听说编译器错误,但我在您的帖子中没有看到任何实际错误。我不是编译器。
  • 您应该使用命名空间。 ofstream 上的 std::ofstream 也不见了。
  • 这似乎成功了,非常感谢您的快速回复!
  • 发布涉及编译器错误的问题时,请始终发布错误消息。它使人们更容易回答问题。
  • 抱歉,下次记得做。

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-09
    • 2017-06-12
    • 2011-07-17
    • 2018-08-21
    相关资源
    最近更新 更多