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