No default instructer exists for (inherited class) c++
程序的目的:这个程序的目的是创建一个只使用控制台的基本二维游戏。程序当前只创建一个带有键"wasd"的可移动框。我计划把子弹,敌人的NPC,障碍物等等都装进去。
问题:您好,出于某种原因,我的"bullet.cpp"文件中的bullet构造函数类出现错误"error c2512:'boxClass':没有合适的默认构造函数可用"。在"bullet.h"中,我有"
其他问题:另外,我应该如何对标题进行分组?他们将成为一个大集群。
评论:我也意识到我的游戏循环不应该在"boxclass.cpp"中。我要在我上完子弹课之后解决这个问题。如果我在构建其他错误的代码,或者如果你看到一种更简单的方法来编写相同的代码,请告诉我。
这是我的第一个C++图形游戏!
控制台应用程序1.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #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/box Speed ="; cin >> speed_Var; BoxClass box1(4, 3); box1.Print_Solid_Rectangle(); cout <<" Move the rectangle with wasd "; //Rectangle Movement box1.Rectangle_Movement(speed_Var); } //exit return 0; } |
箱类.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | #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 <<" "; //draw sides for (height = 1; height < recHeight; height += 1) { cout <<":"; height_Count++; for (width = 1; width < boxSpacesWidth; width += 1) { cout <<""; } cout <<": "; } //draw bottom cout <<":"; for (width = 1; width < boxSpacesWidth; width += 1) { cout <<"."; } cout <<": "; } //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 <<" "; } //draw top of box for (width = 1; width <= horizontalSpaces; width+=1) { cout <<""; } for (width = 1; width < x; width += 1) { cout <<"..."; } cout <<" "; //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 <<": "; } //draw bottom for (width = 1; width <= horizontalSpaces; width += 1) { cout <<""; } cout <<":"; for (width = 1; width < boxSpacesWidth; width += 1) { cout <<"."; } cout <<": "; } 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; }; |
号
H类箱:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #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 |
项目符号.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #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 <<"| "; } } Bullet::Bullet() { } |
。
子弹.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #ifndef BULLET_H #define BULLET_H #include"BoxClass.h" class Bullet : public BoxClass { public: void Bullet_Draw_Collision(); //constructor Bullet(); }; #endif |
控制台窗口.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #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); } |
。
合并窗口.h:
1 2 3 4 5 6 7 8 | #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++中,构造派生类型意味着对象的基和派生部分都必须构造。这是通过在派生类型的构造函数开始之前调用基的构造函数来完成的。
默认行为是使用基的默认构造函数。但是,可以通过将其添加到成员初始化列表并为其提供适当的参数来指示要使用的不同的基构造函数。
在您的情况下,请注意,
通过询问
1 2 3 4 5 6 7 8 9 10 11 | 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 {} |
通过向
1 2 3 4 5 6 7 8 9 10 11 | class Bullet : public BoxClass { public: //constructor Bullet(); }; Bullet::Bullet() : BoxClass(1, 1) {} |
号
或者通过向BoxClass添加一个默认构造函数,例如为每个现有构造函数的参数提供一个默认值:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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 {} |
哪种解决方案最好取决于您的设计目标,但我怀疑第一种解决方案可能更合适。