私は、静的変数を含むいくつかのクラスと一緒に、ファイル内で定義した前方宣言のヘッダーファイル(hook.h)にLRESULT CALLBACK関数を持っています。次に、関連する.cppファイル(hook.cpp)に静的クラス変数を定義(implement/create?)します。ヘッダーファイルのWndProc()に関する問題
最後に、自分のプログラムで使用できるように、stdafx.hファイルにヘッダーファイルを含めます。
私はLRESULTコールバック関数が二度定義されているコンパイルエラーを取得し二回hook.hファイルが含まれているため、エラーは次のとおりです。
stdafx.obj : error LNK2005: "long __stdcall LowLevelKeyboardProc(int,unsigned int,long)" ([email protected]@[email protected]) already defined in main.obj
1>main.obj : error LNK2001: unresolved external symbol "protected: static class LowLevelKeyboardHookEx * LowLevelKeyboardHookEx::instance" ([email protected]@@[email protected])
1>C:\Users\Soribo\Dropbox\C++ Programming\Visual C++ Programming\Key Cataloguer\Release\Key Cataloguer.exe : fatal error LNK1120: 1 unresolved externals
どのように私はこの問題を回避することができますか?
私のヘッダファイル:
#ifndef KEYBOARDHOOK_H
#define KEYBOARDHOOK_H
#include "stdafx.h"
LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam);
class MyClass {
public:
static std::string instanceStr;
// further down this class it refers to the function KeyboardProc() thus need for forward declaration
};
LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam)
{
// implements function
}
#endif
マイhook.cppファイル:
#include "stdafx.h"
#include "hook.h"
std::string MyClass::instanceStr = "";
マイSTDAFX.Hファイル:
#pragma once
#include "targetver.h"
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
// Windows Header Files:
#include <windows.h>
// C RunTime Header Files
#include <stdlib.h>
// Application Specific Includes
#include <string>
#include "hook.h" // I think this is the cause of the error because I include this file twice in compilation which means that the LRESULT function is redefined/reimplemented
を私はまた、フックは含まれません試してみました。 hファイルをhook.cpp &だけstdafx.hを含むが、私は同じ問題を得る?