错误 C2280 / 在 std::vector 中实例化对象时复制构造函数出现问题?
Error C2280 / Problem with Copy Constructor while instantiating an Object in std::vector?
我目前正在按照ChiliTomatoNoodle的教程编写我的第一个应用程序,并根据我的需要修改他的代码。在这样做并实现一个简单的 WindowManager 类时,该类的目的是将所有窗口实例存储在 std::vector 和类似的东西中,我收到以下错误消息:
C:\Program Files (x86(\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\xmemory(758,1(:
错误 C2280:"窗口::窗口(常量窗口 &(":尝试引用已删除的函数(编译源文件 src\cpp\窗口管理器.cpp(
问题似乎出在addWindow
函数中,其中窗口被实例化并存储在std::vector<Window> Wnd
中:
void WindowManager::addWindow(unsigned short x, unsigned short y, unsigned short width, unsigned short height, const char* name, Window::WindowClass& windowClass, DWORD style) {
this->Wnd.emplace_back(Window(name, x, y, width, height, windowClass, style, this->IdGenNum++));
}
我已经将push_back
更改为emplace_back
以避免复制(?(,但它并没有解决问题。
然后还有一个getter(似乎很好,没有复制任何东西(:
Window& WindowManager::getWindow(const unsigned short id) {
for (Window &element : this->Wnd) {
if (element.Id == id) {
return element;
}
}
}
这是Window
类标头:
class Window {
private: // Friends
friend class WindowManager;
public: // Nested Classes
class WindowClass {
...
};
private: // Variables and Instances
unsigned short Id; // Received by WindowManager on Creation
const char* Name;
HWND Handle;
...
public: // Constructors and Deconstructors
Window(const Window&) = delete;
Window(
const char* name,
unsigned short x,
unsigned short y,
unsigned short width,
unsigned short height,
WindowClass& windowClass,
DWORD style,
unsigned short id
);
~Window();
private: // Functions
...
public: // Operators
Window& operator=(const Window&) = delete;
};
编辑:
感谢所有答案和评论,指出参数必须直接传递给emplace_back
方法。事实证明,向量仍然复制了对象(不知道为什么..(,但我可以通过改用std::list
来解决这个问题,它没有这种行为。
Window
复制构造函数是用delete
声明的,但是当你将现有的Window
对象传递到emplace_back()
中时,emplace_back()
的实现将尝试使用其参数就地构造另一个Window
对象。它执行此操作的唯一方法是尝试使用已删除的复制构造函数。
你可以声明一个复制构造函数,使用push_back()
,或者更好地调用emplace_back()
使用另一个Window
构造函数的参数,这将避免复制。
此行创建一个临时窗口,该窗口将被移动(如果有移动构造函数(或复制(如果有复制构造函数(到vector
- 但您的Window
缺少两者:
this->Wnd.emplace_back(Window(name, x, y, width, height, windowClass, style,
this->IdGenNum++));
使用emplace_back
,您无需创建临时参数,因为您提供给emplace_back
的参数完美地转发给Window
的构造函数,因此不会进行不必要的移动或复制:
this->Wnd.emplace_back(name, x, y, width, height, windowClass, style, this->IdGenNum++);
尽管这应该足以解决这个直接问题,但您的Window
类看起来应该支持移动,但不一定支持复制。
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 当从函数参数中的临时值调用复制构造函数时
- 如果有一个模板构造函数只有一个泛型参数,为什么我必须有一个复制构造函数
- 使用复制构造函数复制双精度数组
- C 无可行的构造函数复制类型的变量
- 没有可行的构造函数复制类型 'MyString' 的数组元素
- 编译时,复制构造函数/复制分配和正常功能调用优化之间是否存在任何区别
- 如何最小化调用列表构造函数(复制构造函数)的次数?
- C 11矢量构造函数复制与范围
- 我定义了一个非复制构造函数;复制构造函数还会被隐式定义吗
- 可以将构造函数复制为转换运算符
- 将基类指针的构造函数复制到子类
- C++树类:构造函数/复制/内存泄漏
- 如何制作这个在模板构造函数复制中使用类型定义的类型的模板
- 将构造函数复制为模板化的成员函数
- 绕过私有复制构造函数/复制赋值C++
- C++通过构造函数复制对象
- 复制构造函数 - 复制C++中的对象
- 将带unique_ptr的类的构造函数复制到作为成员的抽象类