LNK2019错误:不可靠的修复" #pragma一次"

时间:2016-06-28 04:41:55

标签: c++ visual-studio debugging linker sdl

编辑:问题是Visual Studio的问题。删除dbFile.h并重新创建fixed the error.

背景:我的主要经验是在C中使用BASH用于学校,并且我在一年多时间内没有大量使用C ++。

我正在创建一个SDL2对象,其中包含打印SDL_GetError()条消息的ofstream文件。我在自己的标头和.cpp文件中都有dbFileApp类。 我为一个带有Window的空SDL项目创建了一个模板,在第一次编译时我得到以下错误。我可以通过在#pragma once中评论/取消注释dbFile.cpp来解决此问题。

1>Debug\dbFile.obj : warning LNK4042: object specified more than once; extras ignored
1>App.obj : error LNK2019: unresolved external symbol "public: __thiscall dbFile::dbFile(void)" (??0dbFile@@QAE@XZ) referenced in function "public: __thiscall App::App(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,unsigned short,unsigned short)" (??0App@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@GG@Z)
1>App.obj : error LNK2019: unresolved external symbol "public: __thiscall dbFile::~dbFile(void)" (??1dbFile@@QAE@XZ) referenced in function __unwindfunclet$??0App@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@GG@Z$1
1>App.obj : error LNK2019: unresolved external symbol "public: void __thiscall dbFile::write(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?write@dbFile@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "public: __thiscall App::App(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,unsigned short,unsigned short)" (??0App@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@GG@Z)
1>F:\SDL\Test\Debug\Test.exe : fatal error LNK1120: 3 unresolved externals

dbFile.h

#ifndef _DB_FILE_H_
#define _DB_FILE_H_

#include <iostream>
#include <fstream>
#include <string>
#include <time.h>

using namespace std;

class dbFile
{
public:
    dbFile(void);
    ~dbFile(void);
    void write(string message);

private:
    ofstream debug;
    time_t t_0;
    char str[26];
    bool inked = false;
};

#endif // !_DB_FILE_H_

dbFile.cpp

#pragma once
#include "dbFile.h"

dbFile::dbFile(void)
{
    debug.open("./Debug.txt", ofstream::out | ios::app);
    t_0 = time(NULL);
}

void dbFile::write(string message)
{
    if (inked == false)
    {
        str[26] = {};
        ctime_s(str, sizeof str, &t_0);
        debug << str << "-------------------------------------------------------\n";
        inked = true;
    }
    debug << message << endl;
}

dbFile::~dbFile(void)
{
    if (inked == true)
    {
        debug << "End of log entry....\n" 
        << "*-----------------------------------------------------*\n\n\n";
    }
    debug.close();
}

App.h

#ifndef _APP_H_
#define _APP_H_

#include <SDL.h>
#include "dbFile.h"

class App
{
public:
    App(string NAME, unsigned short int WIDTH, unsigned short int HEIGHT);
    int GameLoop();
    ~App();

private:

    bool Init(string NAME, unsigned short int WIDTH, unsigned short int HEIGHT);

    unsigned short int  SCREEN_WIDTH    = 800;
    unsigned short int  SCREEN_HEIGHT   = 600;
    bool                QUIT_GAME       = false;

    dbFile          debugFile;
    string          APP_NAME = "\0";

    SDL_Window*     gWindow     = NULL;
    SDL_Surface*    gScreenSurf = NULL;
    SDL_Event       gEvent;
};

#endif // !_APP_H_

App.cpp

#include "App.h"

App::App(string NAME, unsigned short int WIDTH, unsigned short int HEIGHT)
{
    if ((Init(NAME, WIDTH, HEIGHT)) == false)
    {
        debugFile.write("Program failed to initialized... Now closing.");
        return;
    }
}

bool App::Init(string NAME, unsigned short int WIDTH, unsigned short int HEIGHT)
{
    bool bInit = true;

    SCREEN_WIDTH = WIDTH;
    SCREEN_HEIGHT = HEIGHT;
    APP_NAME = NAME;

    if ((SDL_Init(SDL_INIT_VIDEO)) < 0)
    {
        debugFile.write("SDL failed to initialize! Error: ");
        debugFile.write(SDL_GetError());
        bInit = false;
    }

    gWindow = SDL_CreateWindow(APP_NAME.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL);

    if (gWindow == NULL)
    {
        debugFile.write("Failed to create window. Error: ");
        debugFile.write(SDL_GetError());
        bInit = false;
    }

    gScreenSurf = SDL_GetWindowSurface(gWindow);

    if (gScreenSurf == NULL)
    {
        debugFile.write("Failed to create Surface Screen. Error: ");
        debugFile.write(SDL_GetError());
        bInit = false;
    }

    return bInit;
}

int App::GameLoop()
{
    while (QUIT_GAME == false)
    {
        while (SDL_PollEvent(&gEvent) != 0)
        {
            if (gEvent.type == SDL_QUIT)
            {
                QUIT_GAME = true;
            }
        }
    }
    return 0;
}

App::~App()
{
    SDL_DestroyWindow(gWindow);
    SDL_Quit();
}

此代码是&#34;功能正常,&#34;但是我想弄清楚为什么链接器在第一次编译时抱怨。 谢谢!

1 个答案:

答案 0 :(得分:3)

“#pragma once”在头文件中用作#ifdef *** #endif。它不适用于cpp文件。

链接错误是因为链接器无法找到dbFile函数的实现。一旦遇到这些链接器错误,主要是因为您忘记编写实现或忘记链接正确的库。

相关问题