所以我必须使用类和单独的函数制作一个Hangman游戏,我认为我可以更容易地为每个live left制作函数,但当我调用它时它不会打印。。。也许任何人都可以帮我。非常感谢。
This is the main
// TheHangmanGame.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "DrawHangman.h"
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
int main()
{
cout << "WHY wont u WORK!!!! n" ;
cout << "asdf" << &DrawHangman::setDrawMan0 << "n" ;
cout << "work n";
system("PAUSE");
return 0;
}
//--------------------------------------------
//header file
#ifndef DRAWHANGMAN_H
#define DRAWHANGMAN_H
#include "stdafx.h"
#include <string>
using namespace std;
class DrawHangman
{
public:
DrawHangman(char);
void setDrawMan0(char);
int getDrawMan0();
void setDrawMan1(char);
int getDrawMan1();
void setDrawMan2(char);
int getDrawMan2();
void setDrawMan3(char);
int getDrawMan3();
void setDrawMan4(char);
int getDrawMan4();
void setDrawMan5(char);
int getDrawMan5();
void setDrawMan6(char);
int getDrawMan6();
};
#endif
//-------------------------------
//cpp file
#include "stdafx.h"
#include "DrawHangman.h"
#include <iostream>
using namespace std;
DrawHangman::DrawHangman(char)
{
}
void DrawHangman::setDrawMan0(char)
{
using namespace std;
cout << "_______" << endl;
cout << "| |" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
}
int DrawHangman::getDrawMan0()
{
return 1;
}
void DrawHangman::setDrawMan1(char)
{
cout << "_______" << endl;
cout << "| |" << endl;
cout << "| o" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
}
void DrawHangman::setDrawMan2(char)
{
cout << "_______" << endl;
cout << "| |" << endl;
cout << "| o" << endl;
cout << "| /" << endl;
cout << "|" << endl;
cout << "|" << endl;
}
void DrawHangman::setDrawMan3(char)
{
cout << "_______" << endl;
cout << "| |" << endl;
cout << "| o" << endl;
cout << "| /|" << endl;
cout << "|" << endl;
cout << "|" << endl;
}
void DrawHangman::setDrawMan4(char)
{
cout << "_______" << endl;
cout << "| |" << endl;
cout << "| o" << endl;
cout << "| /|\" << endl;
cout << "|" << endl;
cout << "|" << endl;
}
void DrawHangman::setDrawMan5(char)
{
cout << "_______" << endl;
cout << "| |" << endl;
cout << "| o" << endl;
cout << "| /|\" << endl;
cout << "| /" << endl;
cout << "|" << endl;
}
void DrawHangman::setDrawMan6(char)
{
cout << "_______" << endl;
cout << "| |" << endl;
cout << "| o" << endl;
cout << "| /|\" << endl;
cout << "| / \" << endl;
cout << "|" << endl;
}
amp;DrawHangman::setDrawMan0不调用函数,它返回一个指向成员的指针。问题是setDrawManX()函数不是静态的。
您可以将它们设置为静态,也可以创建DrawHangman的实例来调用实例方法。
另外,为什么每个函数都有两个重载?我看不出有任何理由使用未命名的char参数。
您的代码有很多错误。首先,它的设计很糟糕——函数的主要目的之一是为了代码重用和避免重复,但每个set
函数都在复制前一个函数的工作。
除此之外,以下是让代码正常工作(并稍微整理一下)所需的修复:
-
去掉
#include <ctime>
-您没有使用此标头。 -
去掉
#include <string>
-您没有使用此标头。 -
在顶部有
using namespace std
的.cpp
文件中,不要在函数内部重复。 -
在头文件中使用
using namespace std
是不好的做法,因为这会污染包含头的文件的所有封闭命名空间。在任何情况下,您都没有在头文件中使用std
命名空间的任何成员,因此这是毫无意义的。 -
在函数调用之前去掉
&
——该操作符返回函数的地址,这不是您想要的。 -
正确调用函数。对于没有参数的函数,这意味着在函数名称后放置空括号,例如
setDrawMan0()
;对于有参数且没有默认值的函数,则提供参数,例如setDrawMan0(1)
。 -
不要为不使用的函数提供参数。您在
setDrawMan
函数中没有使用char
参数。相反,将函数定义为setDrawMan()
(即没有参数)。 -
您的
getDrawMan
函数似乎没有任何用处,而且您甚至还没有定义其中的大部分。删除它们或给它们一个目的。 -
如果您有一个返回
void
的函数,那么将其流式传输到cout
是没有意义的。您的setDrawMan
函数本身流到cout
,所以只需自己调用该函数,例如setDrawMan0()
(但请参阅下一点)。 -
您的
setDrawMan
函数不访问任何成员变量,因此将它们声明为static
。事实上,您在这里并没有真正以任何有意义的方式使用类——您所做的只是将一组相关的函数收集在一起。这也可以通过将它们作为命名空间内声明的自由函数来实现。 -
如果你真的想使用类(而且你的作业似乎需要这样做),那么你就大错特错了。与其有一堆不同的硬编码绘制函数,不如有一个类来跟踪成员变量中还剩多少生命。然后有一个单独的绘制函数,它检查生命的数量并相应地绘制。
调用"setDrawMan"作为标准的c函数不是更好吗?
代替
.h
void setDrawMan0(char);
main
cout << "asdf" << &DrawHangman::setDrawMan0 << "n" ;
类型
.h
void setDrawMan0(void);
main
cout << "asdf" << DrawHangman::setDrawMan0() << "n" ;
通过这种方式,可以更简单地解释代码并预测其行为。