【问题标题】:LNK2019 Error: Unreliable Fix "#pragma once" [closed]LNK2019 错误:不可靠的修复“#pragma once”[关闭]
【发布时间】:2016-06-28 04:41:55
【问题描述】:

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

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

我正在创建一个 SDL2 对象,其中包含打印 SDL_GetError() 消息的流文件。我在自己的头文件和 .cpp 文件中有一个 dbFileApp 类。 我为带有窗口的空 SDL 项目创建了一个模板,在第一次编译时出现以下错误。我可以通过在 dbFile.cpp 中评论/取消评论 #pragma once 来解决它。

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();
}

这段代码是“功能性的”,但我想弄清楚为什么链接器在第一次编译时抱怨。 谢谢!

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 我知道 pragma 不用于 .cpp 文件,但它修复了 LNK 错误。据我所知,我包含正确的头文件。编辑-原来是 Visual Studio 的问题。我将 .cpp 重命名为 .h 文件。删除 dbFile.h 并创建一个新文件解决了这个问题。感谢您的评论!
    猜你喜欢
    • 2012-06-02
    • 1970-01-01
    • 1970-01-01
    • 2011-02-27
    • 2012-10-01
    • 2010-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多