结构内部有结构,类之间互访
来源:互联网 发布:ubuntu killall 编辑:程序博客网 时间:2024/05/17 07:38
源文件
main.cc
testclass.cc
playerlist.cc
头文件
testclass.hh
playerlist.hh
main用到testclass.cc而testclass.cc多个成员函数需要访问playerlist.cc的多个成员函数
因为在playerlist.cc中建了个动态表,动态表由多次new创建,所以需要在deconstructor里用delete将它们一一删除
我一开始 在playerlist.hh 中是这么写的结构
struct NodeType {
struct component {
string name;
int numberofGame;
int totalmoney;
} ;
NodeType *link;
};
结果编译器说源文件中的CurrPtr->component.name invalid
后来照着一位牛人说的改成
struct NodeType {
struct {
string name;
int numberofGame;
int totalmoney;
} component;
NodeType *link;
};
就没有错误了
下面是代码
在playerlist.hh中
#ifndef PLAYERLIST_HH
#define PLAYERLIST_HH
#include<string>
#include<iostream>
#include<cstring>
using namespace std;
struct NodeType {
struct {
string name;
int numberofGame;
int totalmoney;
} component;
NodeType *link;
};
typedef NodeType* NodePtr;
class playerlist {
private:
NodePtr head;
NodePtr currPtr;
NodePtr newNodePtr;
public:
int isEmpty() const;
void printallinfo() ;
void printname() ;
void insertNode (string gameplayer, int money, int game);
void deleteTop();
bool find(string playername) ;
playerlist();
~playerlist();
};
#endif
//------------------------------------------END------------------------
在playerlist.cc中
#include "playerlist.hh"
playerlist::playerlist()
{
head = NULL;
}
int playerlist::isEmpty() const
{
return (head == NULL);
}
void playerlist::printallinfo()
{
currPtr = head;
if (currPtr != NULL ) {
cout << "Name\t" <<"number of Games \t" << " total Money\n";
}
while(currPtr != NULL) {
cout << currPtr->component.name << "\t";
cout << currPtr->component.numberofGame << "\t";
cout << currPtr->component.totalmoney << endl;
currPtr = currPtr->link;
}
if (currPtr == NULL) {
cout << "No player record!\n";
}
}
// Check if the player's name exists
// exist, return true
// not exist, return false
bool playerlist::find(string playername)
{
currPtr = head;
while (currPtr != NULL) {
if (strcmp(currPtr->component.name.c_str(),playername.c_str())==0) {
return 1;
}
else
return 0;
}
return 0;
}
//
void playerlist::printname()
{
currPtr = head;
while(currPtr != NULL) {
cout << currPtr->component.name << endl;
currPtr = currPtr->link;
}
}
void playerlist::insertNode (string playername, int money, int game) // what is the parameter?
{
currPtr = head;
NodePtr prevPtr = NULL;
newNodePtr = new NodeType;
newNodePtr->component.totalmoney = money; // money is item?
newNodePtr->component.name = playername;
newNodePtr->component.numberofGame = game;
// while head is not NULL
// then prevPtr becomes head
// currPtr becomes what head pointed at
while(currPtr != NULL ) {
prevPtr = currPtr;
currPtr = currPtr->link;
}
// newNodePtr also pointed at currPtr
// so newNodePtr becomes head
newNodePtr->link = currPtr;
// while() loop if didn't happen
// which means prevPtr is NULL
// newNodePtr becomes the head
if (prevPtr ==NULL) {
head = newNodePtr;
}
// don't understand here, why newNodePtr cannot be head
else {
prevPtr->link = newNodePtr;
}
}
void playerlist::deleteTop()
{
if (head) {
NodePtr temPtr = head;
head = head->link;
delete temPtr;
}
}
playerlist::~playerlist()
{
while(!isEmpty()) {
deleteTop();
}
}
在testclass.hh中
#include "playerlist.hh"
playerlist::playerlist()
{
head = NULL;
}
int playerlist::isEmpty() const
{
return (head == NULL);
}
void playerlist::printallinfo()
{
currPtr = head;
if (currPtr != NULL ) {
cout << "Name\t" <<"number of Games \t" << " total Money\n";
}
while(currPtr != NULL) {
cout << currPtr->component.name << "\t";
cout << currPtr->component.numberofGame << "\t";
cout << currPtr->component.totalmoney << endl;
currPtr = currPtr->link;
}
if (currPtr == NULL) {
cout << "No player record!\n";
}
}
// Check if the player's name exists
// exist, return true
// not exist, return false
bool playerlist::find(string playername)
{
currPtr = head;
while (currPtr != NULL) {
if (strcmp(currPtr->component.name.c_str(),playername.c_str())==0) {
return 1;
}
else
return 0;
}
return 0;
}
//
void playerlist::printname()
{
currPtr = head;
while(currPtr != NULL) {
cout << currPtr->component.name << endl;
currPtr = currPtr->link;
}
}
void playerlist::insertNode (string playername, int money, int game) // what is the parameter?
{
currPtr = head;
NodePtr prevPtr = NULL;
newNodePtr = new NodeType;
newNodePtr->component.totalmoney = money; // money is item?
newNodePtr->component.name = playername;
newNodePtr->component.numberofGame = game;
// while head is not NULL
// then prevPtr becomes head
// currPtr becomes what head pointed at
while(currPtr != NULL ) {
prevPtr = currPtr;
currPtr = currPtr->link;
}
// newNodePtr also pointed at currPtr
// so newNodePtr becomes head
newNodePtr->link = currPtr;
// while() loop if didn't happen
// which means prevPtr is NULL
// newNodePtr becomes the head
if (prevPtr ==NULL) {
head = newNodePtr;
}
// don't understand here, why newNodePtr cannot be head
else {
prevPtr->link = newNodePtr;
}
}
void playerlist::deleteTop()
{
if (head) {
NodePtr temPtr = head;
head = head->link;
delete temPtr;
}
}
playerlist::~playerlist()
{
while(!isEmpty()) {
deleteTop();
}
}
//-----------------------END----------------------------------
在testclass.hh中
#ifndef TESTCLASS_HH
#define TESTCLASS_HH
#include "playerlist.hh"
#include<string>
using namespace std;
class testclass
{
public:
testclass();
void insert(string,int,int);
bool find(string);
private:
playerlist players;
};
#endif
//------------------------------END--------------------------
在testclass.cc中
#include "testclass.hh"
testclass::testclass()
{
// playerlist players;
}
void testclass::insert(string person, int money, int game)
{
players.insertNode(person,money,game);
}
bool testclass::find(string person)
{
return (players.find(person));
}
//---------------------------------END-----------------------
在main.cc中
#include<iostream>
#include "testclass.hh"
#include<iostream>
using namespace std;
int main()
{
testclass test;
bool shouldbe = 0;
test.insert("Tom",1000,5);
shouldbe = test.find("Jerry");
cout << shouldbe << endl;
return 0;
}
//--------------------------------------END---------------------------
- 结构内部有结构,类之间互访
- CNN、RNN、DNN的内部网络结构有什么区别?
- 单片机内部接口结构
- Webkit内部模块结构
- JavaScript 内部对象结构
- 内部类外部类之间的互访
- 利用反射强制转换结构相同的类(类之间无需有继承关系)
- 利用反射强制转换结构相同的类(类之间无需有继承关系)
- DIV+CSS 让左右结构内容之间有一定距离
- 在类和结构之间选择
- Scala 控制结构内部揭密
- 详解mysql 内部编码结构
- MFC内部组织原理结构
- 揭秘chromium内部目录结构
- 揭秘chromium内部目录结构
- FPGA的内部组成结构
- 插件之间的互访
- CNN(卷积神经网络)、RNN(循环神经网络)、DNN(深度神经网络)的内部网络结构有什么区别?
- 用gdb调试core dump文件
- MFC常用控件一
- java基础面试题剖析
- DOM应用---遍历网页中的元素
- Mysql 远程连接错误:1130-host IP is not allowed to connect to this MySql server
- 结构内部有结构,类之间互访
- 利用虚拟化服务器节省成本
- 【搜索之BFS】杭电 hdu 1548 A strange lift
- android的socket程序中conn.getResponseCode() 为405
- hdoj 3660 Alice and Bob's Trip
- vimwiki textile
- MFC常用控件二
- #pragma pack(push,1)与#pragma pack(1)的区别
- 最大公约数(Greatest Common Divisor ) 与 最小公倍数(Lowest Common Multiple )