c++ - C++没有构造函数实例匹配参数列表e0289
问题描述
所以代码应该可以工作,但事实并非如此。我该如何解决? 在此处输入图像描述
#include <iostream>
class Button{
private:
unsigned width;
unsigned height;
public:
Button(): width(0), height(0){};
Button(unsigned _width, unsigned _height):
width(_width), height(_height){};
unsigned getWidth(){ return width; };
unsigned getHeight(){ return height; };
void setWidth(unsigned _width){ width = _width; };
void setHeight(unsigned _height){ height = _height; };
};
class Window{
protected:
Button button;
int x;
int y;
public:
Window(){
x = y = 0;
}
Window(int _x, int _y, Button _button):
x(_x), y(_y), button(_button){};
~Window(){
x = 0;
y = 0;
}
};
class Menu: public Window{
private:
char *title;
public:
Menu() = default;
Menu(char* _title, int _x, int _y, Button _button):
title(_title), Window(_x, _y, _button){
std::cout << "Menu has been created." << std::endl;
};
~Menu(){
title = NULL;
std::cout << "Menu has been deleted." << std::endl;
}
friend std::ostream& operator<<(std::ostream& os, Menu& menu){
os << "Button \"" << menu.title << "\" on (" << menu.x << "," << menu.y << ") with size " << menu.button.getWidth() << "x" << menu.button.getHeight() << ".";
return os;
}
};
int main(){
Button button(10, 10);
Menu menu("A main menu", 5, 5, button);
std::cout << menu << std::endl;
return 0;
}
解决方案
至少声明数据title
成员
const char *title;
和构造函数
Menu( const char* _title, int _x, int _y, Button _button):
Window(_x, _y, _button), title(_title) {
std::cout << "Menu has been created." << std::endl;
};
因为 C++ 中的字符串文字(与 C 相对)具有常量字符数组的类型。
尽管使用类型而不是const char *
数据成员的类型title
会更好std::string
std::string title.
推荐阅读
- reactjs - 如何在 React.js 中使用 Material 的 UI
- java - 你如何在array.length中重复这个方法?
- javascript - 在按 Enter 的同时单击按钮
- r - RMarkdown - print() 命令导致成套 PDF 中的页面边界线超出
- swift - Swift:有没有办法从连续的 swift 文件中初始化数组条目?
- python - 将字符串内容转换为列表
- material-ui - 当 body 有 dir = 'rtl' 时,Material UI 电话号码混乱
- r - data.table 的 frollmean() 和 forecast 的 ma() 之间略有不同,顺序是偶数
- postgresql - 无法在 Mac 终端 ZSH 中打开 Postgres shell
- php - Laravel 问题,如何在 Blade 上使用 _GET