我正在尝试创建一个使用标头(.h)文件和源(.cpp)文件的类。我遇到了这个似乎无法解决的错误:
错误:“Menu :: Menu(int w,int h)”没有提供初始化器:
这是我的代码:
部首:
//Menu.h:
#ifndef MENU_H
#define MENU_H
#include <StdAfx.h>
#include <objidl.h>
#include <gdiplus.h>
#include <windows.h>
using namespace Gdiplus;
#pragma comment (lib,"Gdiplus.lib")
class Menu
{
public:
Menu(int w, int h);
void render();
void checkInput(int x, int y, int message);
void setWidth(int w);
void setHeight(int h);
void setOpenWidth(int w);
void setOpenHeight(int h);
void setPosX(int x);
void setPosY(int y);
void setDraggablePaneColor(Color c);
void setContentPaneColor(Color c);
void setCornerButtonColorInactive(Color c);
void setCornerButtonColorActive(Color c);
void setTextColor(Color c);
void setBorderColor(Color c);
private:
//Position variables:
//window position variables
int posX;
int posY;
//drag offset variables
int dragX;
int dragY;
//Width and height variables:
//width and height of draggable pane
int width;
int height;
//width and height of content pane
int widthOpen;
int heightOpen;
//States
//menu open states
bool menuOpen;
//corner button hover states
bool cornerButtonHover;
bool cornerButtonWasHovering;
//dragging state
bool dragging;
//left mouse button down state
bool lmbDown;
//Colors
//draggable pane color
Color draggablePaneColor;
//content pane color
Color contentPaneColor;
//inactive button color (not hovering)
Color cornerButtonColorInactive;
//active button color (hovering)
Color cornerButtonColorActive;
//text color
Color textColor;
//border color
Color borderColor;
//Constants
//corner button text
const wchar_t cornerButtonText[][3];
//corner button length
const int cornerButtonLength;
//Content
//element content[];
};
#endif
来源:
//Menu.cpp
#include "Menu.h"
Menu::Menu(int w, int h)
{
}
int posX = 0;
int posY = 0;
//drag offset variables
int dragX = 0;
int dragY = 0;
//Width and height variables:
//width and height of draggable pane
int width = 150;
int height = 20;
//width and height of content pane
int widthOpen = 150;
int heightOpen = 200;
//States
//menu open states
bool menuOpen = true;
//corner button hover states
bool cornerButtonHover = false;
bool cornerButtonWasHovering = false;
//dragging state
bool dragging = false;
//left mouse button down state
bool lmbDown = false;
//Colors
//draggable pane color
Color draggablePaneColor = Color(60, 60, 60);
//content pane color
Color contentPaneColor = Color(80, 80, 80);
//inactive button color (not hovering)
Color cornerButtonColorInactive = Color(60, 60, 60);
//active button color (hovering)
Color cornerButtonColorActive = Color(70, 70, 70);
//text color
Color textColor = Color::White;
//border color
Color borderColor = Color::Black;
//Constants
//corner button text
const wchar_t cornerButtonText[][3] = {L"+", L"-", L"X"};
//corner button length
const int cornerButtonLength = height - 4;
//Content
//element content[];
void Menu()
{
}
答案 0 :(得分:0)
const
个成员。你可能认为你已经在做那件事,但事实并非如此。你的代码:
Menu::Menu(int w, int h)
{
}
int posX = 0;
int posY = 0;
...
定义 empty 构造函数,然后在文件范围定义新变量。 posX
创建的int posX = 0;
与Menu::posX
无关。要在构造函数中正确初始化成员变量,需要使用以下命令:
Menu::Menu(int w, int h)
{
posX = 0;
posY = 0;
...
}