C++类:概念类兴奋剂:LNK2005

C++ Classes: Conceptual Class Doppelganger: LNK2005

本文关键字:LNK2005 兴奋剂 C++      更新时间:2023-10-16

Sheesh。我在定义类时遇到了臭名昭著的LNK2005错误,我似乎无法解决这个问题。

(我正在撕下我的一个同样臭名昭著的单身汉来好好反思,组织。)最初说的单身汉是编码的。。。以这样一种发散的、辉煌的方式。。。为了避免我当时不知道的所有C++OPP原理和概念,但它起了作用。。。。以某种方式尽管有一些最简单的C++概念的偶然性,但它确实做到了。现在,我需要组织、编译速度和先进的结构化技术来使它快速工作,而且——你明白了

不管怎样。在把它拆开,不得不重写一些之后,我注意到了一个必要性。我必须声明多个.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已更改为显示Niall的解决方案。编辑:

问题:

我收到LNK2005问题是因为我在它们的头文件中自动初始化了类,考虑到堆栈溢出问题的解决方案:

VS 2010 C++LNK2005在使用#pragma一次和#ifndef 时出错

根据我对解决方案的理解,这似乎不起作用。

编译器

VS2013退货:

Error 1 error LNK2005: "class Direct3D Direct3D" (?Direct3D@@3V0@A) already defined in Direct3D.obj C:UsersInfinityMachinedocumentsvisual studio 2013ProjectsWin32Project3Win32Project3Win32Project3.obj Win32Project3

Error 2 error LNK2005: "class Windows Windows" (?Windows@@3V0@A) already defined in Win32Project3.obj C:UsersInfinityMachinedocumentsvisual studio 2013ProjectsWin32Project3Win32Project3Windows.obj Win32Project3

Error 3 error LNK1169: one or more multiply defined symbols found C:UsersInfinityMachinedocumentsvisual studio 2013ProjectsWin32Project3DebugWin32Project3.exe 1 1 Win32Project3

立即声明class Direct3D... } Direct3D;和同名变量。这就是造成这种错误的原因。

Direct3D类型的变量,命名为Direct3D(或其他),将在包括该标头的每个翻译单元中,Windows也是如此。

一种可能的解决方案是删除变量声明,另一种是将其移动并使其静态,或者在匿名命名空间中。备选方案包括extern或实现单例;这可能更接近最初的意图。

在进一步讨论的基础上,这里有一个适当的解决方案;

class Direct3D {}; 
extern Direct3D direct3D;

然后在其中一个实现文件中

Direct3D direct3D;

然后,它将对象声明为extern,并提供它的一个实例。