這裏的程序指針就給賽格故障
#include <iostream>
#include <time.h>
#include <conio.h>
#include <cstdlib>
#include <windows.h>
using namespace std;
const int widht = 117,height = 26;
class Paddle
{
private:
int y;
int originalY;
public:
Paddle()
{
y=height/2-2;
originalY=y;
}
inline int getY()
{
return y;
}
inline void moveUp()
{
y--;
}
inline void moveDown()
{
y++;
}
void checkWall()
{
if (y<0)
{
while (y<0)
{
y++;
}
}
else if (y>height)
{
while (y>height)
{
y--;
}
}
}
void Reset()
{
y=originalY;
}
};
class Ball
{
private:
int x,y;
int originalX,originalY;
public:
Ball()
{
x=widht/2;
y=height/2;
originalX=x;
originalY=y;
}
inline int getX()
{
return x;
}
inline int getY()
{
return y;
}
inline void moveRight()
{
x++;
}
inline void moveUpRight()
{
x++;
y--;
}
inline void moveDownRight()
{
x++;
y++;
}
inline void moveLeft()
{
x--;
}
inline void moveUpLeft()
{
y--;
x--;
}
inline void moveDownLeft()
{
y++;
x--;
}
inline void Reset()
{
x=originalX;
y=originalY;
}
};
class Manager
{
private:
int score1,score2;
int columns, rows;
public:
int p1y;
int p2y;
int ballX,ballY;
bool gameOver;
Manager()
{
gameOver = false;
}
void Draw(Paddle *p1,Paddle *p2,Ball *b)
{
system("cls");
p1y=p1->getY();
p2y=p1->getY();
ballX=b->getX();
ballY=b->getY();
for (int i=0;i<height;i++)
{
for (int j=0;j<widht;j++)
{
if (i==p1y && j==2)
{
cout << "\xDB";
}
else if (i==p1y+1 && j==2)
{
cout << "\xDB";
}
else if (i==p1y+2 && j==2)
{
cout << "\xDB";
}
else if (i==p1y+3 && j==2)
{
cout << "\xDB";
}
else if (i==p1y+4 && j==2)
{
cout << "\xDB";
}
else if (i==p2y && j==widht-1)
{
cout << "\xDB";
}
else if (i==p2y+1 && j==widht-1)
{
cout << "\xDB";
}
else if (i==p2y+2 && j==widht-1)
{
cout << "\xDB";
}
else if (i==p2y+3 && j==widht-1)
{
cout << "\xDB";
}
else if (i==p2y+4 && j==widht-1)
{
cout << "\xDB";
}
else if (i==ballX && j==ballY)
{
cout << "O";
}
cout << " ";
}
cout << endl;
}
cout << p1 -> getY();
}
void Input(Paddle *p1,Paddle *p2)
{
if (_kbhit())
{
switch(_getch())
{
case 'w':
p1->moveUp();
break;
case 's':
p1->moveDown();
break;
case 'i':
p2->moveUp();
break;
case 'k':
p2->moveDown();
break;
}
}
}
void Run(Paddle *p1,Paddle *p2, Ball *b)
{
while(!gameOver)
{
Draw(p1,p2,b);
Input(p1,p2);
Sleep(10);
}
}
};
int main()
{
Paddle *p1;
Paddle *p2;
Ball *b;
Manager *m;
m->Run(p1,p2,b);
return 0;
}
我不明白爲什麼當我啓動程序給它(與調試器)分段錯誤。 我認爲原因是指針,因爲在它完美工作之前(但沒有修改值); Hae的任何提示?
你有沒有嘗試用調試器通過你的代碼? – Ceros
歡迎來到Stack Overflow。如果你還沒有閱讀,請閱讀[關於]頁面。但更迫切的是,請閱讀如何創建一個MCVE([MCVE])。目前還不清楚你的代碼是否很小 - 看起來相當長。 –
只給出你認爲給出錯誤的部分代碼。 – AkaSh