Sheesh。我在定義我的課時碰到臭名昭着的LNK2005
錯誤,我似乎無法解決問題。C++類:概念類Doppelganger:LNK2005
(我正撕裂一個同樣臭名昭着的單身人士,以反映出良好的組織結構。)最初的單身人士被編碼......以這樣一種發散的,輝煌的方式......以避免所有我當時無知的C++ OPP原則和概念,但它工作正常! ....不知何故。儘管它有一些最簡單的C++概念。現在我需要組織,編譯速度和先進的結構化技術,以使其工作得更快,並且 - 您得到它。
A-無論如何。分裂後,不得不重寫一些,我已經注意到了必要性。我必須聲明多個.cpp
文件,只是因爲編譯器被雙重聲明和通常的頭類定義大規模地加重了。
此外,我已根據情況使用預處理器指令。但是仍有一些錯誤。
注意(編輯):我已經重寫了提供錯誤的問題。
考慮:
D3D.h
#include "Infinity.h"
class Direct3D :
public Infinity
{
public:
Direct3D();
~Direct3D();
IDXGISwapChain *Swapchain; //Display modes.
static ID3D11Device *Device;
static ID3D11DeviceContext *DeviceContext;
static ID3D11RenderTargetView *RenderTargetView;
void D3D_Start(float width, float height);
void D3D_Render();
void D3D_Terminate();
void ViewPort(float Height, float Width, float MaxDepth, float MinDepth, float TopLeftX, float TopLeftY);
}Direct3D;
...和Windows.h
#include "Infinity.h"
class Windows :
public Infinity
{
public:
Windows();
~Windows();
bool DisplayWindow(int width, int height, HINSTANCE hInstance);
static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
}Windows;
最後,Infinity.h
#pragma once
class Infinity{
public:
Infinity();
~Infinity();
static HWND hWnd;
};
而所有的實現都在它們各自的.cpp文件中。除#pragma
外,我用#ifndef
... #endif
。我懷疑我可能會無意中通過自動初始化頭文件中的類來調用某種實現。但它看起來瘋狂潔淨,並允許我宣佈函數成員爲:
Direct3D.D3D_Start()
沒有說明靜態成員Direct3D::D3D_Start()
。 我的標題都應該是靜態的嗎?
編輯:下面,.cpp
文件:
#include "stdafx.h"
#include "Infinity.h"
#include "Windows.h"
#include "Direct3D.h"
MSG msg;
float width = 1024;
float height = 768;
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
windows.DisplayWindow(1280, 900, hInstance);
direct3D.D3D_Start(width, height);
direct3D.ViewPort(height, width, 1.0f, 0.0f, 0, 0);
while (WM_QUIT != msg.message){
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else{
direct3D.D3D_Render();
}
}
direct3D.D3D_Terminate();
return msg.wParam;
}
*更新.cpp
已更改爲尼爾展示的解決方案。 編輯:
問:
我收到了LNK2005問題,因爲在他們的頭文件我自動初始化我的班,考慮在堆棧溢出問題的解決方案:
VS 2010 C++ LNK2005 errors while using #pragma once and #ifndef
哪個沒有按根據我對解決方案的理解,似乎不起作用。
編譯
VS2013回報:
Error 1 error LNK2005: "class Direct3D Direct3D" ([email protected]@[email protected]) already defined in Direct3D.obj C:\Users\InfinityMachine\documents\visual studio 2013\Projects\Win32Project3\Win32Project3\Win32Project3.obj Win32Project3
Error 2 error LNK2005: "class Windows Windows" ([email protected]@[email protected]) already defined in Win32Project3.obj C:\Users\InfinityMachine\documents\visual studio 2013\Projects\Win32Project3\Win32Project3\Windows.obj Win32Project3
Error 3 error LNK1169: one or more multiply defined symbols found C:\Users\InfinityMachine\documents\visual studio 2013\Projects\Win32Project3\Debug\Win32Project3.exe 1 1 Win32Project3
這並不完全清楚你在問什麼。 – 2014-08-31 19:46:57
@RSahu我編輯了澄清的主要問題。 – 2014-08-31 19:52:23
'我必須聲明多個.cpp文件'一個不聲明「.cpp文件」。一個聲明一個類,一個函數或一個變量。 '我的頭文件應該都是靜態的嗎?'沒有靜態頭文件。有靜態類成員。 'Main.h'「Infinity.h」在哪裏? '我得到的LNK2005問題'沒有你得到的錯誤列表(逐字!)和沒有MCVE(注意所有字母都很重要),我們只能做出沒有教養的猜測。 – 2014-08-31 19:56:31