Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Linker error LNK2005/1169 when using smart pointer C++

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"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);
}

>Solution :

You include you 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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading