1

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

}
4

1 回答 1

0

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

Menu::Menu(int w, int h)
{

}

int posX = 0;
int posY = 0;
...

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

Menu::Menu(int w, int h)
{
   posX = 0;
   posY = 0;
   ...
}
于 2013-02-27T01:17:42.567 回答