结构内部有结构,类之间互访

来源:互联网 发布: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---------------------------

原创粉丝点击