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

C2661 when using std::make_unique

I have an error when I am trying to use std::make_unique. I probably have made a very simple mistake and I would be thankful if someone pointed it out. I am using GLAD and GLFW as 3rd party libraries. I get an error code of C2661 when I run it, C2661 ‘Window::Window’: no overloaded function takes 3 arguments on the next code snippet in window.cpp.

return std::make_unique<Window>(std::move(nativeWindow), width, height);
Window.hpp
#pragma once

#include <memory>
#include <string>

#include <GLFW/glfw3.h>

struct Window final {
public:
    std::unique_ptr<Window> createWindow(int width, int height, std::string title);

    bool shouldClose() { return glfwWindowShouldClose(nativeWindow); }

    void swapBuffers() { glfwSwapBuffers(nativeWindow); }
private:
    GLFWwindow* nativeWindow;
    int width;
    int height;
    std::string title;
};

Window.cpp
#include "Window.hpp"

std::unique_ptr<Window> Window::createWindow(int width, int height, std::string title) {
    nativeWindow = glfwCreateWindow(width, height, title.c_str(), nullptr, nullptr);
    glfwMakeContextCurrent(nativeWindow);

    return std::make_unique<Window>(std::move(nativeWindow), width, height);
}

sorry for bad code quality.

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

>Solution :

This line:

std::make_unique<Window>(std::move(nativeWindow), width, height);

assumes your Window type has the following constructor available:

Window::Window(GLFWwindow* win, int width, int height)

This can be fixed by adding the correct constructor (I’ve also passed through the title, which you weren’t storing anywhere).

Window.hpp

#pragma once

#include <memory>
#include <string>

#include <GLFW/glfw3.h>

struct Window final {
public:


    // constructor for Window object
    Window(GLFWwindow* win, int w, int h, std::string title) 
       : nativeWindow(win), width(w), height(h), title(title)
    {
    }

    // I assume you meant for this method to be static?
    // It makes very little (if any sense) as a member function.
    static 
    std::unique_ptr<Window> createWindow(int width, int height, std::string title);

    bool shouldClose() { return glfwWindowShouldClose(nativeWindow); }

    void swapBuffers() { glfwSwapBuffers(nativeWindow); }
   


private:
    GLFWwindow* nativeWindow;
    int width;
    int height;
    std::string title;
};
#include "Window.hpp"

std::unique_ptr<Window> Window::createWindow(int width, int height, std::string title) {
    nativeWindow = glfwCreateWindow(width, height, title.c_str(), nullptr, nullptr);
    glfwMakeContextCurrent(nativeWindow);

    return std::make_unique<Window>(nativeWindow, width, height, title);
}

Then you should be call the method like so:

std::unique_ptr<Window> ptr = Window::createWindow(640, 480, "hello") 
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