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.
>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")