c++ 将函数放在类中
c++ Putting a function in a class
嗨,我是 c++ 的新手,已经开始研究类。
我在下面有这个代码用于一个简单的迷宫游戏。
#include <iostream>
using namespace std;
void print(char maze[10][9])
{
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 9; j++)
{
cout << maze[i][j];
}
cout << endl;
}
}
int main()
{
char maze[][9] = { { '+', '-', '-', '-', '-', '-', '-', '-', '+' },
{ '|', ' ', ' ', ' ', '|', ' ', ' ', ' ', '|' },
{ '|', ' ', '|', ' ', ' ', ' ', '|', ' ', '|' },
{ '|', ' ', '+', '-', '-', '-', '+', ' ', '|' },
{ '|', '#', '|', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', '-', '+', ' ', '+', ' ', '+', '-', '|' },
{ '|', ' ', ' ', ' ', '|', ' ', '|', ' ', '|' },
{ '|', ' ', '|', ' ', '+', '-', '+', ' ', '|' },
{ '|', '*', '|', ' ', ' ', ' ', ' ', ' ', '|' },
{ '+', '-', '-', '-', '-', '-', '-', '-', '+' } };
int row = 8;
int col = 1;
bool gameOver = false;
while(!gameOver)
{
print(maze);
char move = ' ';
cout << "Move: ";
while(move != 'w' && move != 's' && move != 'a' && move != 'd' && move != 'q')
{
cin >> move;
}
if (move == 'w')
{
if (maze[row - 1][col] == ' ')
{
maze[row][col] = ' ';
row--;
maze[row][col] = '*';
}
else if (maze[row - 1][col] == '#')
{
gameOver = true;
}
}
else if (move == 's')
{
if (maze[row + 1][col] == ' ')
{
maze[row][col] = ' ';
row++;
maze[row][col] = '*';
}
else if (maze[row + 1][col] == '#')
{
gameOver = true;
}
}
else if (move == 'a')
{
if (maze[row][col - 1] == ' ')
{
maze[row][col] = ' ';
col--;
maze[row][col] = '*';
}
else if (maze[row][col - 1] == '#')
{
gameOver = true;
}
}
else if (move == 'd')
{
if (maze[row][col + 1] == ' ')
{
maze[row][col] = ' ';
col++;
maze[row][col] = '*';
}
else if (maze[row][col + 1] == '#')
{
gameOver = true;
}
}
else if (move == 'q')
{
gameOver = true;
}
else
{
cout << "Invalid Input" << endl;
}
}
return 0;
}
与其将所有这些代码都放在 Main 函数 ID 中,不如将其作为一个类。这是我尝试过的。
新迷宫.cpp文件
#include <iostream>
#include "maze.h"
using namespace std;
void print(char maze[10][9])
{
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 9; j++)
{
cout << maze[i][j];
}
cout << endl;
}
}
int main()
{
maze mazenow;
mazenow.mazegame();
cout << "Hi world" << endl;
}
还有我创建的头文件,maze.h
#include <iostream>
using namespace std;
class maze
{
public:
int mazegame(){
char maze[][9] = { { '+', '-', '-', '-', '-', '-', '-', '-', '+' },
{ '|', ' ', ' ', ' ', '|', ' ', ' ', ' ', '|' },
{ '|', ' ', '|', ' ', ' ', ' ', '|', ' ', '|' },
{ '|', ' ', '+', '-', '-', '-', '+', ' ', '|' },
{ '|', '#', '|', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', '-', '+', ' ', '+', ' ', '+', '-', '|' },
{ '|', ' ', ' ', ' ', '|', ' ', '|', ' ', '|' },
{ '|', ' ', '|', ' ', '+', '-', '+', ' ', '|' },
{ '|', '*', '|', ' ', ' ', ' ', ' ', ' ', '|' },
{ '+', '-', '-', '-', '-', '-', '-', '-', '+' } };
int row = 8;
int col = 1;
bool gameOver = false;
while(!gameOver)
{
print(maze);
char move = ' ';
cout << "Move: ";
while(move != 'w' && move != 's' && move != 'a' && move != 'd' && move != 'q')
{
cin >> move;
}
if (move == 'w')
{
if (maze[row - 1][col] == ' ')
{
maze[row][col] = ' ';
row--;
maze[row][col] = '*';
}
else if (maze[row - 1][col] == '#')
{
gameOver = true;
}
}
else if (move == 's')
{
if (maze[row + 1][col] == ' ')
{
maze[row][col] = ' ';
row++;
maze[row][col] = '*';
}
else if (maze[row + 1][col] == '#')
{
gameOver = true;
}
}
else if (move == 'a')
{
if (maze[row][col - 1] == ' ')
{
maze[row][col] = ' ';
col--;
maze[row][col] = '*';
}
else if (maze[row][col - 1] == '#')
{
gameOver = true;
}
}
else if (move == 'd')
{
if (maze[row][col + 1] == ' ')
{
maze[row][col] = ' ';
col++;
maze[row][col] = '*';
}
else if (maze[row][col + 1] == '#')
{
gameOver = true;
}
}
else if (move == 'q')
{
gameOver = true;
}
else
{
cout << "Invalid Input" << endl;
}
}
return 0;
}
}
我得到的错误,
缺少 ; 使用前
和
未找到"打印"标识符
一个菜鸟问题,但帮助解决这个问题将是很好的:D我什至不知道我试图做的事情是否有意义,如果没有,请道歉
编辑:重组,我认为现在更正确
类声明末尾缺少一个";":
class maze
{
public:
[...]
}; // Here !
要在maze.h
代码中使用打印函数,您需要先有一个print
函数的原型。但是print
迷宫类的私有成员函数而不是独立函数会更简单,这将使您能够从类的其他函数调用它:
class maze
{
private:
void print(char maze[10][9])
[...]
public:
int mazegame()
[...]
}; // Still here !
在 class maze
中使用之前,必须声明void print(char maze[10][9])
。此外,它要求有一个与其类(class maze
和char[][9] maze::maze
(同名的成员是麻烦的。如果从未更改maze::maze
,则应将其设置为const
成员,以便在您意外尝试修改它时编译器会抱怨。最后,永远不要在头文件中说using namespace std;
(最好也不要在源文件中(,请参阅此问题以了解原因。
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- 将对象数组的引用传递给函数
- 函数调用中参数的顺序重要吗
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用不带参数的函数访问结构元素
- 代码在main()中运行,但在函数中出现错误
- 内置函数可查看CPP中的成员变量
- 如何获取std::result_of函数的返回类型
- 如何在c++中为模板函数实例创建快捷方式
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗