C++ LNK2019错误:不可靠的修复”#布拉格语一次“;

C++ LNK2019错误:不可靠的修复”#布拉格语一次“;,c++,visual-studio,debugging,linker,sdl,C++,Visual Studio,Debugging,Linker,Sdl,编辑:问题是Visual Studio的问题。删除并重新创建dbFile.h 背景:我的主要经验是在 BASH < /代码>中使用 C ,我在一年多时间内还没有使用C++。 我正在创建一个SDL2对象,该对象包含和流文件,该文件打印SDL\u GetError()消息。我有一个dbFile和App类,每个类都有自己的头文件和.cpp文件。 我为一个带有窗口的空SDL项目创建了一个模板,在第一次编译时,我得到了以下错误。我可以通过在dbFile.cpp中注释/取消注释#pragma一次来解决这个问

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

背景:我的主要经验是在 BASH < /代码>中使用<代码> C <代码>,我在一年多时间内还没有使用C++。 我正在创建一个SDL2对象,该对象包含和流文件,该文件打印
SDL\u GetError()
消息。我有一个
dbFile
App
类,每个类都有自己的头文件和.cpp文件。 我为一个带有窗口的空SDL项目创建了一个模板,在第一次编译时,我得到了以下错误。我可以通过在
dbFile.cpp
中注释/取消注释
#pragma一次来解决这个问题

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
1>Debug\dbFile.obj:警告LNK4042:多次指定对象;多余的被忽略了
1> App.obj:错误LNK2019:未解析的外部符号“public:u thiscall dbFile::dbFile(void)”(??0dbFile@@QAE@XZ)在函数“public:u thiscall App::App(类std::basic_string,无符号短,无符号短)”中引用(??0App@@QAE@V?$basic_string@DU?$char_traits@D@性病病毒$allocator@D@2@@std@@GG@Z)
1> App.obj:错误LNK2019:未解析的外部符号“public:u thiscall dbFile::~dbFile(void)”(??1dbFile@@QAE@XZ)在函数uu unwindfunclet$??0App中引用@@QAE@V?$basic_string@DU?$char_traits@D@性病病毒$allocator@D@2@@std@@GG@Z$1
1> App.obj:错误LNK2019:未解析的外部符号“public:void\u thiscall dbFile::write(class std::basic\u string)”(?write@dbFile@@QAEXV?$basic_string@DU?$char_traits@D@性病病毒$allocator@D@函数“public:\u thiscall App::App(类std::basic\u string,unsigned short,unsigned short)”中引用了2@@std@@@Z)(??0App)@@QAE@V?$basic_string@DU?$char_traits@D@性病病毒$allocator@D@2@@std@@GG@Z)
1> F:\SDL\Test\Debug\Test.exe:致命错误LNK1120:3个未解析的外部
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_
#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_
\ifndef\u DB\u文件_
#定义数据库文件_
#包括
#包括
#包括
#包括
使用名称空间std;
类dbFile
{
公众:
数据库文件(无效);
~dbFile(无效);
无效写入(字符串消息);
私人:
流调试;
时间t_0;
char-str[26];
bool-inked=false;
};
#endif/!\u数据库文件_
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();
}
#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();
}
#pragma一次
#包括“dbFile.h”
dbFile::dbFile(无效)
{
打开(“./debug.txt”,of stream::out | ios::app);
t_0=时间(空);
}
void dbFile::write(字符串消息)
{
如果(墨迹==假)
{
str[26]={};
ctime_s(str、str大小和t_0);
debug“#pragma once”用作头文件中的#ifdef***#endif。它不用于cpp文件


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

我知道pragma不用于.cpp文件,但它修复了LNK错误。据我所知,我有正确的h包含了eader文件。编辑-这是Visual Studio的一个问题。我将.cpp重命名为.h文件。删除了dbFile.h并创建了一个新文件解决了此问题。感谢您的评论!