我正在编写一个版本的刽子手作为作业,我已经对这些指示感到困惑。但是,我想将STL列表传递给我创建的类的函数。为了在不同的状态下打印我的刽子手,例如头部,身体,左臂等。我的代码大部分都是:
#include <iostream>
#include <string>
#include <list>
#include "slot.h"
using namespace std;
int main()
{
string head[4] = {"\t\t *****", "\t\t * *", "\t\t * *",
"\t\t *****"};
string body[10] = {"\t\t *****", "\t\t * *", "\t\t * *",
"\t\t *****", "\t\t |", "\t\t |",
"\t\t |", "\t\t |", "\t\t |"};
string leftarm[10] = {"\t\t *****", "\t\t * *", "\t\t * *",
"\t\t *****", "\t\t\\ |", "\t\t \\ |",
"\t\t \\|", "\t\t |", "\t\t |", "\t\t |"};
string rightarm[10] = {"\t\t *****", "\t\t * *", "\t\t * *",
"\t\t *****", "\t\t \\ | /", "\t\t \\ | /",
"\t\t \\|/", "\t\t |", "\t\t |",
"\t\t |"};
string leftleg[13] = {"\t\t *****", "\t\t * *", "\t\t * *",
"\t\t *****", "\t\t \\ | /", "\t\t \\ | /",
"\t\t \\|/", "\t\t |", "\t\t |",
"\t\t |", "\t\t /", "\t\t /", "\t\t /",};
string rightleg[13] = {"\t\t *****", "\t\t * *", "\t\t * *",
"\t\t *****", "\t\t \\ | /", "\t\t \\ | /",
"\t\t \\|/", "\t\t |", "\t\t |",
"\t\t |",
"\t\t / \\",
"\t\t / \\",
"\t\t / \\",};
string holdword = "";
list<string> hanglist;
for (int j = 0; j < 4; j++) {
holdword = head[j];
hanglist.push_back(holdword);
holdword = "";
}
for (int j = 0; j < 10; j++) {
holdword = body[j];
hanglist.push_back(holdword);
holdword = "";
}
for (int j = 0; j < 10; j++) {
holdword = leftarm[j];
hanglist.push_back(holdword);
holdword = "";
}
for (int j = 0; j < 10; j++) {
holdword = rightarm[j];
hanglist.push_back(holdword);
holdword = "";
}
for (int j = 0; j < 13; j++) {
holdword = leftleg[j];
hanglist.push_back(holdword);
holdword = "";
}
for (int j = 0; j < 13; j++) {
holdword = rightleg[j];
hanglist.push_back(holdword);
holdword = "";
}
DisplayHangman(hanglist);
Slot temp;
temp.DisplayHang(hanglist);
hanglist.clear();
return 0;
}
这是类和头文件:
//********************************************
// This slot.cpp class is designed and created
// to supply the appropriate observer and
// mutator methods.
//********************************************
#include <iostream>
#include <list>
#include <string>
#include "slot.h"
using namespace std;
Slot::Slot()
{
list<string> holdlist(60);
}
void DisplayHangman(list<string>& hanglist) {
holdlist = hanglist;
string hangword = "";
int lstsize = 0;
lstsize += hanglist.size();
for (int i = 0; i < lstsize; i++) {
string diphang = holdlist.front();
holdlist.pop_front();
hangword = diphang;
cout << hangword << endl;
hangword = "";
}
lstsize = 0;
holdlist.clear();
}
Slot::~Slot() {}
#include <iostream>
#include <list>
#include <string>
class Slot
{
public:
Slot();
void DisplayHangman(list<string>& hanglist);
private:
list<string> holdlist(60);
};
Slot::~Slot()
我很沮丧,我真的打了一张桌子,手骨折了! 这是否符合我的想法?
将列表作为参数传递给已从另一个类中抽象出来的函数?
我只是想对它进行单元测试。
答案 0 :(得分:0)
DisplayHangman
和slot
中使用的main()
未被声明。Slot
没有会员DisplayHang
。Slot:Slot()
中有一个无意义的声明和初始化局部变量。void DisplayHangman(list<string>& hanglist)
slot.cpp
不是成员函数的定义。
标题中的(60)
将发出编译错误。list
和string
。Slot::~Slot()
不是它应该的位置。更正后的代码:
main()
函数的最后一部分:
for (int j = 0; j < 13; j++) {
holdword = rightleg[j];
hanglist.push_back(holdword);
holdword = "";
}
Slot temp;
temp.DisplayHangman(hanglist);
hanglist.clear();
return 0;
}
slot.cpp
中的函数定义:
Slot::Slot() : holdlist(60)
{
}
void Slot::DisplayHangman(list<string>& hanglist) {
// implementation should be same as the original, omitted
}
Slot::~Slot() {}
头文件:
#include <list>
#include <string>
class Slot
{
public:
Slot();
~Slot();
void DisplayHangman(std::list<std::string>& hanglist);
private:
std::list<std::string> holdlist;
};