没有用于初始化类的匹配构造函数
No matching constructor for initialisation of class
看不出我做错了什么...
我的游戏窗口.h有代码 -
#include <iostream>
#include <string>
using namespace::std;
class GameWindow{
string WindowType;
string WindowName;
int TopLeftX,TopLeftY;
int Height,Width;
bool GetBoxed;
public:
GameWindow(string WinType, string WinName,int TLX,int TLY,int y,int x,bool box);
void SetHeight(int y);
void SetWidth(int x);
void SetTopLeftY(int TLY);
void SetTopLeftX(int TLY);
};
源文件代码是——
#include "GameWindow.h"
GameWindow::GameWindow(string WinType, string WinName,int TLX,int TLY,int y,int x,bool box){
WindowType = WinType; WindowName = WinName;
TopLeftX = TLX; TopLeftY = TLY;
Height = y; Width = x;
GetBoxed = box;
};
void GameWindow::SetHeight(int y){Height = y;}
void GameWindow::SetWidth(int x){Width = x;}
void GameWindow::SetTopLeftY(int TLY){TopLeftY=TLY;}
void GameWindow::SetTopLeftX(int TLX){TopLeftX=TLX;}
所以在另一个源文件中,我尝试创建一个 GameSpace 的向量,并在每次向向量添加一个时调用构造函数 -
int OffsetX =5;
vector<GameWindow>GameSpace;
GameSpace.resize(8);
GameSpace[0] = GameWindow("MonstersLeftWin", "Misc",
(getmaxx(stdscr)-22-OffsetX), 2, 1, 22, true);
我收到"没有匹配的游戏窗口初始化构造函数"错误。根本看不出我做错了什么!
如果我有 - 我也会收到错误 -
GameSpace[0] = *new GameWindow("MonstersLeftWin", "Misc",
(getmaxx(stdscr)-22-OffsetX), 2, 1, 22, true);
仍然不确定我是否需要那里的"新"。
感谢您的帮助。
这里的问题是GameSpace.resize(8)
语句。一旦您提供了自定义构造函数,编译器将不再为您生成默认构造函数。但是,std::vector
将在调整大小调用中默认初始化元素。
提供默认构造函数(首选):GameWindow()
或者使用void resize (size_type n, const value_type& val);
重载
在此语句中
GameSpace.resize(8);
编译器尝试调用类游戏窗口的默认构造函数。但是,该类没有默认构造函数。因此,编译器会发出错误。
而不是
GameSpace.resize(8);
例如,您可以编写以下内容
GameSpace.resize( 8, { "", "", 0, 0, 0, 0, false } );
相关文章:
- 没有用于初始化C++中的变量模板的匹配构造函数
- 用于创建命名构造函数的宏
- 没有用于调用默认构造函数的匹配函数
- std::vector 是否有用于引用的复制构造函数?
- 没有用于初始化模拟工厂的匹配构造函数
- 模板 ctor 类型推导不起作用(没有匹配的构造函数用于初始化 ...)与函数<>参数
- 没有适用于 std::unique_ptr 的适当默认构造函数
- 具有字符串文本构造函数的类不适用于 const 引用初始化
- 用于赋值的参数化构造函数
- C++构造函数,一个用于度,一个用于弧度
- 用于 protobuf 转换的大括号初始化样式构造函数
- 使用构造函数作为成员函数的 C++(用于序列化)
- 构造函数不能用于启发性化
- 错误:没有用于初始化和mexfunction的匹配构造函数
- 没有用于初始化可变参数模板类的匹配构造函数
- ifstream 没有用于初始化的匹配构造函数
- 将 SFINAE 用于模板化构造函数时出现问题
- 在已发布的结构中添加构造函数(用于 memcpy)是否安全?
- 没有匹配的构造函数用于初始化模板化类的构造函数
- “没有匹配的构造函数用于初始化”与矢量