2016-06-28 17 views
-1

編集:この問題はVisual Studioの問題でした。 dbFile.hを削除し、それをfixed the error.LNK2019エラー:Unreliable Fix "#pragma once"

背景を再現:私の主な経験は、学校のためにBASHCを使用している、と私は重く年以上にC++を使用していません。

私は、SDL_GetError()メッセージを出力するofstreamファイルを含むSDL2オブジェクトを作成しています。私はそれぞれdbFileAppクラスを独自のヘッダーと.cppファイルに持っています。 ウィンドウを持つ空の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)" ([email protected]@[email protected]) 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)" ([email protected]@[email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]) 
1>App.obj : error LNK2019: unresolved external symbol "public: __thiscall dbFile::~dbFile(void)" ([email protected]@[email protected]) referenced in function [email protected]@[email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]$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> >)" ([email protected]@@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@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)" ([email protected]@[email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]) 
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(); 
} 

このコードは、「機能的」であるが、私は、リンカが最初のコンパイルに不平を言っている理由を把握したいと思います。 ありがとう!

答えて

3

ヘッダファイルに#ifdef *** #endifとして "#pragma once"が使用されています。これはcppファイルではありません。

リンクエラーは、リンカーがdbFileの関数の実装を見つけることができないためです。いったんこれらのリンカーエラーに遭遇したのは、ほとんどの場合、実装を書くのを忘れたり、正しいライブラリをリンクするのを忘れたりしたためです。

+0

プラグマは.cppファイルには使用されていませんが、LNKエラーを修正したものです。私の知る限り、正しいヘッダーファイルが含まれています。編集 - それはVisual Studioの問題であることが判明しました。 .cppの名前を.hファイルに変更しました。 dbFile.hを削除し、問題を解決した新しいファイルを作成します。コメントありがとう! –

関連する問題