在源文件中定义方法时出错

Error When Defining Method In Source File

本文关键字:出错 方法 定义 源文件      更新时间:2023-10-16

我正在尝试创建一个使用头(.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()
{
}

const成员必须在构造函数中显式初始化。你可能认为你已经在做了,但事实并非如此。您的代码:

Menu::Menu(int w, int h)
{
}
int posX = 0;
int posY = 0;
...

定义一个构造函数,然后在文件范围内定义新变量。由int posX = 0;创建的posXMenu::posX无关。要在构造函数中正确初始化成员变量,您需要使用以下内容:

Menu::Menu(int w, int h)
{
   posX = 0;
   posY = 0;
   ...
}