看不出我做错了什么......
我的 GameWindow.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 vector ,并在每次向 vector 添加一个时调用构造函数 -
int OffsetX =5;
vector<GameWindow>GameSpace;
GameSpace.resize(8);
GameSpace[0] = GameWindow("MonstersLeftWin", "Misc",
(getmaxx(stdscr)-22-OffsetX), 2, 1, 22, true);
我收到“没有匹配的 GameWindow 初始化构造函数”错误。根本看不出我做错了什么!
如果我有 - 我也会收到错误 -
GameSpace[0] = *new GameWindow("MonstersLeftWin", "Misc",
(getmaxx(stdscr)-22-OffsetX), 2, 1, 22, true);
仍然不确定我是否需要那里的"new"。
感谢您的帮助。
最佳答案
这里的问题是 GameSpace.resize(8)
语句。一旦您提供了自定义构造函数,编译器就不会再为您生成默认构造函数。但是,std::vector
将在调整大小调用中默认初始化元素。
要么提供默认构造函数(首选):GameWindow()
或者使用void resize (size_type n, const value_type& val);
重载
关于c++ - 类的初始化没有匹配的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21796348/