2014-08-31 90 views
0

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

+1

這並不完全清楚你在問什麼。 – 2014-08-31 19:46:57

+0

@RSahu我編輯了澄清的主要問題。 – 2014-08-31 19:52:23

+0

'我必須聲明多個.cpp文件'一個不聲明「.cpp文件」。一個聲明一個類,一個函數或一個變量。 '我的頭文件應該都是靜態的嗎?'沒有靜態頭文件。有靜態類成員。 'Main.h'「Infinity.h」在哪裏? '我得到的LNK2005問題'沒有你得到的錯誤列表(逐字!)和沒有MCVE(注意所有字母都很重要),我們只能做出沒有教養的猜測。 – 2014-08-31 19:56:31

回答

0

您聲明立即class Direct3D... } Direct3D;和同一個名字的變量。這將是這種錯誤的原因。

類型爲Direct3D的變量名爲Direct3D(或以其他方式)將包含在包含該標題的每個翻譯單元中,這同樣適用於Windows

一個可能的解決方案是刪除變量聲明,另一個可能是將其移動並使其變爲靜態或匿名命名空間。替代項包括extern或實施單身人士;這可能更接近最初的意圖。

根據進一步的討論,這裏有一個合適的解決方案;

class Direct3D {}; 
extern Direct3D direct3D; 

然後在實現文件

Direct3D direct3D; 

這的一個,然後聲明對象extern,並提供了一個單一實例。

+0

將直接聲明從'Direct3D'更改爲'direct3D'導致類似:錯誤LNK2005:「Direct3D direct3D」(?direct3D @@ 3VDirect3D @@ A)已在Direct3D.obj中定義了錯誤\t C: \ Users \ InfinityMachine \ documents \ visual studio 2013 \ Projects \ Win32Project3 \ Win32Project3 \ Win32Project3.obj \t Win32Project3'該問題已被編輯以顯示主'.cpp'。 – 2014-08-31 20:39:14

+0

錯誤不是變量的名稱,而是它的位置。在類聲明之後嘗試'static Direct3D Direct3D;'。 – Niall 2014-08-31 20:40:59

+0

如果其他地方不需要,使'_tWinMain'函數的本地'Direct3D'對象。 – Niall 2014-08-31 20:45:30