Home > Back-end >  Linker error LNK2005/1169 when using smart pointer C
Linker error LNK2005/1169 when using smart pointer C

Time:08-16

So I'm separating class definition and implementation.

  • Platform: Windows x64
  • Compiler: MSVC
  • IDE: VS 2022

Error "resolves" when I use inline keyword before smart pointer, but I want to understand the problem-

Error codes:

"class std::unique_ptr<class Window,struct std::default_delete<class Window> > loaderWindow" (?loaderWindow@@3V?$unique_ptr@VWindow@@U?$default_delete@VWindow@@@std@@@std@@A) already defined"

Removed all code unrelated to error.

Header file:

#pragma once

class Window {
public:
    Window() noexcept;
};

auto loaderWindow = std::make_unique<Window>();

Source file:

Window::Window() noexcept
{
    MessageBox(NULL, L"Ctor called", L"OK", MB_OK);
}

CodePudding user response:

You include your header file in more than one source files. This is why you see this error message. Remove the definition of the global variable from your header file, or include it in one source file only.

  • Related