在源文件中定义方法时出错
Error When Defining Method In Source File
我正在尝试创建一个使用头(.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;
创建的posX
与Menu::posX
无关。要在构造函数中正确初始化成员变量,您需要使用以下内容:
Menu::Menu(int w, int h)
{
posX = 0;
posY = 0;
...
}
相关文章:
- LNK2019 在文件中调用静态成员的方法时出错.cpp
- 将私有数据成员作为默认参数传递给该类的公共方法时出错
- 方法的参数列表出错
- 从同一类中的另一个方法调用方法时出错
- r-使用Rcpp模块公开带有引用参数的c++类方法时出错
- 链接webrtc本机时出错,因为未定义对具有std::string的方法的引用
- 使用字符串调用方法时出错:类型为'size_t和
- Inventor.h中矢量的添加方法出错
- C++ 中头文件中的 Add 方法出错
- 我正在尝试创建一个方法,该方法使用指针算术返回一对,它是否会出错
- 从c++调用非静态java方法时出错
- 调用简单getter方法后出错:basic_string::_S_create
- C++ 当我将父方法作为派生对象调用时出错
- 添加到方法的 C++ 自定义异常会出错
- g++4.8.2上列表方法参数默认初始化时出错
- 设置方法C++时出错
- 在C++11中存储对方法的引用时出错
- c++私有属性可以在构造函数中使用,但seg在其他方法中会出错
- 使用静态方法时出错
- 未使用的模板方法中出错