对于(继承类)C ,不存在默认的仪器
No default instructer exists for (inherited class) c++
程序的目的:此程序的目的仅使用Consol创建基本的2D游戏。该程序当前仅创建一个带有键" WASD"的可移动盒。我打算使子弹,敌人的NPC,障碍等等。
问题:您好,由于某种原因,我的" Bullet.cpp"文件中的子弹构造函数类别显示出错误"错误" C2512:'BoxClass':没有适当的默认构造函数可用"。在"子弹"中,我有" class Bullet : protected BoxClass
"。我为什么会遇到此错误?
其他问题:我还应该如何对标头进行分组?他们正在成为一个大集群。
注释:我还意识到我的游戏循环不应该在" BoxClass.cpp"中。我将在我的子弹课上工作后解决这个问题。如果我构成其他错误的问题,或者您看到了一种更简单的编写一些相同代码的方法,请告诉我。
这是我在C !
中的第一个图形游戏consolapplication1.cpp:
#include "stdafx.h"
#include <windows.h>
#include "BoxClass.h"
#include "ConsolWindow.h"
#include "Bullet.h"
#include <iostream>
using namespace std;
#define W_KEY 0x57
#define S_KEY 0x53
#define A_KEY 0x41
#define D_KEY 0x44
#define R_KEY 0x52
int main() {
//variable declaration/definition
int right_Wall, speed_Var;
//Consol Size/Position
int half_screen_Size = (GetSystemMetrics(SM_CXSCREEN)/2);
Set_Consol_Size(half_screen_Size, GetSystemMetrics(SM_CYSCREEN));
Position_Consol(-6, 0);
while (1) {
cout << "Enter speed of rectangle/boxnSpeed = ";
cin >> speed_Var;
BoxClass box1(4, 3);
box1.Print_Solid_Rectangle();
cout << "nnMove the rectangle with wasdnn";
//Rectangle Movement
box1.Rectangle_Movement(speed_Var);
}
//exit
return 0;
}
boxclass.cpp:
#include "stdafx.h"
#include "BoxClass.h"
#include "ConsolWindow.h"
#include <iostream>
#include <math.h>
#include <Windows.h>
using namespace std;
#define W_KEY 0x57
#define S_KEY 0x53
#define A_KEY 0x41
#define D_KEY 0x44
#define R_KEY 0x52
#define _NOT_MOVING 0
#define _MOVING 1
//Non Moving Rectangle
void BoxClass::Print_Solid_Rectangle() {
//calc
boxSpacesWidth = (3 * recWidth) - 4;
//draw top of box
for (width = 1; width < recWidth; width += 1) {
cout << "...";
}
cout << "n";
//draw sides
for (height = 1; height < recHeight; height += 1) {
cout << ":";
height_Count++;
for (width = 1; width < boxSpacesWidth; width += 1) {
cout << " ";
}
cout << ":n";
}
//draw bottom
cout << ":";
for (width = 1; width < boxSpacesWidth; width += 1) {
cout << ".";
}
cout << ":n";
}
//Moving Rectangle
void BoxClass::Print_Rectangle_Moving(int x, int y, int horizontalSpaces, int verticleSpaces) {
//calc
boxSpacesWidth = (3 * x) - 4;
rightWall = ((x-1)*3) + horizontalSpaces;
retrieveX = (ceil((((x-1)*3)/2))+1)+horizontalSpaces;
retrieveY = verticleSpaces;
cout << retrieveY<<endl;
//New Line
for (i = 1; i <= verticleSpaces; i += 1) {
cout << "n";
}
//draw top of box
for (width = 1; width <= horizontalSpaces; width+=1) {
cout << " ";
}
for (width = 1; width < x; width += 1) {
cout << "...";
}
cout << "n";
//draw sides
for (height = 1; height < y; height += 1) {
for (width = 1; width <= horizontalSpaces; width += 1) {
cout << " ";
}
cout << ":";
height_Count++;
for (width = 1; width < boxSpacesWidth; width += 1) {
cout << " ";
}
cout << ":n";
}
//draw bottom
for (width = 1; width <= horizontalSpaces; width += 1) {
cout << " ";
}
cout << ":";
for (width = 1; width < boxSpacesWidth; width += 1) {
cout << ".";
}
cout << ":n";
}
void BoxClass::Rectangle_Movement(int speed) {
speed_Var = speed;
//Rectangle Movement
while (GetAsyncKeyState(VK_ESCAPE) == false) {
if (GetAsyncKeyState(R_KEY)) {
system("CLS");
break;
}
if (GetAsyncKeyState(W_KEY)) {
if (verticleCount > 0) {
system("CLS");
verticleCount = verticleCount - speed_Var;
Print_Rectangle_Moving(recWidth, recHeight, horizontalCount, verticleCount);
}
}
if (GetAsyncKeyState(S_KEY)) {
system("CLS");
verticleCount = verticleCount + speed_Var;
Print_Rectangle_Moving(recWidth, recHeight, horizontalCount, verticleCount);
}
if (GetAsyncKeyState(A_KEY)) {
if (horizontalCount > 0) {
system("CLS");
horizontalCount = horizontalCount - (speed_Var*2);
Print_Rectangle_Moving(recWidth, recHeight, horizontalCount, verticleCount);
}
}
if (GetAsyncKeyState(D_KEY)) {
if (rightWall < 113) {
system("CLS");
horizontalCount = horizontalCount + (speed_Var*2);
Print_Rectangle_Moving(recWidth, recHeight, horizontalCount, verticleCount);
}
}
if (GetAsyncKeyState(VK_SPACE)) {
}
}
}
// constructor
BoxClass::BoxClass(int x, int y) {
//variable definition
height_Count = 1;
speed_Var = 1;
horizontalCount = 0;
verticleCount = 0;
recWidth = x;
recHeight = y;
};
boxclass.h:
#ifndef BOXCLASS_H
#define BOXCLASS_H
class BoxClass {
unsigned short int width;
int height, i, recWidth, recHeight, rightWall;
float boxSpacesWidth, height_Count;
int width_Var, height_Var, position_Var;
int speed_Var = 1;
unsigned short int horizontalCount = 0, verticleCount = 0;
protected:
//retrieve values for bullet spawn location
int retrieveX, retrieveY;
public:
void Print_Rectangle_Moving(int x, int y, int horizontalSpaces, int verticleSpaces);
void Print_Solid_Rectangle();
void Rectangle_Movement(int speed);
// constructor
BoxClass(int x, int y);
};
#endif
bullet.cpp:
#include "stdafx.h"
#include "Bullet.h"
#include <iostream>
#include <Windows.h>
using namespace std;
void Bullet::Bullet_Draw_Collision() {
for (int height = 1; height <= 2; height+=1) {
cout << "|n";
}
}
Bullet::Bullet() {
}
bullet.h:
#ifndef BULLET_H
#define BULLET_H
#include "BoxClass.h"
class Bullet : public BoxClass {
public:
void Bullet_Draw_Collision();
//constructor
Bullet();
};
#endif
consolwindow.cpp:
#include "stdafx.h"
#include "ConsolWindow.h"
#include <iostream>
#include <Windows.h>
using namespace std;
int Find_Consol_Size() {
CONSOLE_SCREEN_BUFFER_INFO csbi;
int columns, rows;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
return columns;
}
void Set_Consol_Size(int x, int y) {
HWND console = GetConsoleWindow();
RECT r;
GetWindowRect(console, &r);
MoveWindow(console, r.left, r.top, x, y, TRUE);
}
void Position_Consol(int x, int y) {
HWND consoleWindow = GetConsoleWindow();
SetWindowPos(consoleWindow, 0, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
}
consolwindow.h:
#ifndef CONSOLWINDOW_H
#define CONSOLWINDOW_H
int Find_Consol_Size();
void Set_Consol_Size(int x, int y);
void Position_Consol(int x, int y);
#endif
在C 中,构建派生类型意味着必须构建对象的基础和派生部分。这是通过在派生类型的构造函数开始之前调用基数的构造函数来完成的。
默认行为是使用基数的默认构造函数。但是,您可以通过将其添加到成员初始化列表中并为其提供适当的参数来指示要使用的不同的基本构造函数。
在您的情况下,请注意,BoxClass
只有一个构造函数,该构造函数必须必须使用x
和y
,但Bullet
没有这样的构造参数。也许某些默认值(例如零或一个值(可能是合适的,尽管我怀疑您需要将这些参数添加到Bullet
类中。这里有一些示例来说明如何解决问题。
通过要求Bullet
的施工论证:
class Bullet : public BoxClass
{
public:
//constructor
Bullet(int x, int y);
};
Bullet::Bullet(int x, int y) :
BoxClass(x, y) // Indicates which BoxClass constructor to use
{}
通过向BoxClass
提供任意施工论证:
class Bullet : public BoxClass
{
public:
//constructor
Bullet();
};
Bullet::Bullet() :
BoxClass(1, 1)
{}
或通过向BoxClass添加默认构造函数,例如通过为其现有构造函数的每个构造函数提供默认值:
class BoxClass
{
public:
// This constructor is now a default constructor
BoxClass(int x = 1, int y = 1);
};
class Bullet : public BoxClass
{
public:
//constructor
Bullet();
};
Bullet::Bullet() // Uses the default BoxClass constructor
{}
哪种解决方案最佳取决于您的设计目标,但我怀疑第一个解决方案可能更合适。
- 我们可以访问一个不存在的联盟的成员吗
- C++:对不存在的命名空间使用命名空间指令
- g++ 说函数不存在,即使包含正确的标头
- 从具有默认值的部分指定模板类继承时发生SWIG错误,具有不带默认值的正向声明
- 显式 std::exception_ptr 转换为 bool 不存在.VS2010 错误?
- C++ 尝试在不存在的构造函数中引用已删除的函数(使用 rapidJson)
- 查找第一个数组中不存在的元素
- 编译器错过了无效的构造函数调用,并调用不存在的(或私有的)默认构造函数
- 对于(继承类)C ,不存在默认的仪器
- C 类不存在默认的构造函数
- 为什么C 11将不存在的值定义为默认值
- 错误:类不存在默认构造函数
- 子 - c++ 不存在默认构造函数
- 类不存在默认构造函数,但传递了非默认构造函数
- 不存在的标识符在非启用函数模板中用作默认参数
- 类 X(继承)C++不存在默认构造函数
- log4cpp - log4cpp::AppenderSkeleton 不存在默认构造函数
- 类不存在默认构造函数的错误
- 有没有一种方法可以测试默认构造函数是否不存在
- 没有合适的默认构造函数可用,也不存在默认构造函数