Aggregation and function template question

Hello
Ive been using a mud source code which is written in c to learn how to code. I am trying to convert it to c++ as i go.
My first question
The class declarations below have a mix of class data members, is this aggregation? i googled for a few hours today on the topic and didnt have much luck finding a good explanation. Through some trial an error i worked out how the class declarations worked which in turn made it much easier to setup the doubly linked lists which leads to my next question.
The function templates
I would like to make the templates a little more versatile and pass 5 arguments to them instead of 3

like this:
LINK(pObj, ch->first_content, ch->last_content, next_content, prev_content);
or
LINK(pObj, ch->first_content, ch->last_content, next, prev);

then i can change the templates to
1
2
3
4
5
6
7
8
9
void LINK(T1& link, T2& first, T2& last, next, prev)
{
    if (!first)         first = link;
    
    else              last->next = link;
    link->next = NULL;
    link->prev = last;
    last = link;
}


Ive tried a variety of different syntaxes but with no luck just a lot of
undeclared (first use this function)
errors
Any suggestions or a link to a related topic would be greatly appreciated.
Being a novice any comments on the code below that help me improve would also be appreciated..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <limits>
using namespace std;

typedef class Room_Data;
typedef class Char_Data;

// adds obj to dll
template <typename T1, typename T2>
void LINK(T1& link, T2& first, T2& last)
{
    if (!first)         first = link;
    
    else              last->next_content = link;
    link->next_content = NULL;
    link->prev_content = last;
    last = link;
}

// removes obj from dll
template <typename T1, typename T2>
void UNLINK(T1& link, T2& first, T2& last)
{
     if (!link->prev_content)     
        first = link->next_content;
     
     else     
              link->prev_content->next_content = link->next_content;
     
     if (!link->next_content)    
        last = link->prev_content;
     
     else     
              link->next_content->prev_content = link->prev_content;
}

class Obj_Data
{
      public:
             Obj_Data *  next;
             Obj_Data *  prev;
             Obj_Data *  next_content;         //dll iterators for rooms and charact inv
             Obj_Data *  prev_content;         //"                                      "
             Char_Data * carried_by;
             Room_Data * in_room;
             char *      name;
}*pObj;

class Room_Data
{
      public:
             Room_Data() { first_content = NULL; last_content = NULL;}
             ~Room_Data() {};
             Obj_Data * first_content;   //objects inroom
             Obj_Data * last_content;    //"             "
             int        vnum;
};

class Char_Data
{
      public:
             Char_Data() { first_carry = NULL; last_carry = NULL;}
             ~Char_Data() {};
             Obj_Data *  first_carry;
             Obj_Data *  last_carry;
             char *      name;
};

// adds object to room dll
void move_obj(Room_Data *pRoom, char * name)
{
     pObj = new Obj_Data;
     pObj->name = name;
     LINK(pObj, pRoom->first_content, pRoom->last_content);           //adds object to room
     pObj->in_room = pRoom;
     pObj->carried_by = NULL;
     return;
}

// adds obj to characters dll 
void add_obj(Char_Data *ch, char * name)
{
     pObj = new Obj_Data;
     pObj->name = name;
     LINK(pObj, ch->first_carry, ch->last_carry);                     //adds object to char inv
     pObj->carried_by = ch;
     pObj->in_room = NULL;
     return;
}

int main()
{
    Room_Data *pRoom;
    pRoom = new Room_Data;
    pRoom->vnum = 2400;
    
    Char_Data *ch;
    ch = new Char_Data;
    ch->name = "Jim";
    
    add_obj(ch, "pouch");
    add_obj(ch, "box");
    add_obj(ch, "sword");
    move_obj(pRoom, "pipe");

    cout << "Press ENTER to quit." << flush;
    cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
    return 0;
}
In C++ the standard libraries come with a linked list built in.

1
2
3
#include <list>

std::list<Obj_Data> obj;


You might benefit from looking at that and seeing if you can utilise it:

http://www.cplusplus.com/reference/stl/list/

Thanks Galik , i had experimented with stl lists before without luck but i gave them ago again after seeing your reply and there exactly what i need.

here is another go at the above code with stl list

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <iterator>
#include <list>
#include <limits>

using std::cout;
using std::endl;
using std::list;

template<typename T1, typename T2>
void add_to_list(T1& arg1, T2& arg2)
{
     arg1->Obj.push_back(*arg2);
     return;
}

template<typename T>
void show_list(T& arg)
{
     for (arg->iter = arg->Obj.begin(); arg->iter != arg->Obj.end(); ++arg->iter)
     {
         std::cout<<arg->iter->name<<std::endl;
         }
}

class Obj_Data
{
      public:
            char *      name;
            list<Obj_Data>Obj;
            list<Obj_Data>::iterator iter;
}*pObj;

class Char_Data
{
      public:
             char *     name;
             list<Obj_Data>Obj;
             list<Obj_Data>::iterator iter;
}*ch;

class Room_Data
{
      public:
             int vnum;
             list<Obj_Data>Obj;
             list<Obj_Data>::iterator iter;
}*pRoom;

int main()
{
    
    pRoom = new Room_Data;
    pRoom->vnum = 2400;
    
    pObj = new Obj_Data;
    pObj->name = "Chest";
    add_to_list(pRoom, pObj);
    
    show_list(pRoom);
    
    ch = new Char_Data;
    ch->name = "jim";
    
    pObj = new Obj_Data;
    pObj->name = "Pouch";
    add_to_list(ch, pObj);
    
    pObj = new Obj_Data;
    pObj->name = "Box";
    add_to_list(ch, pObj);
      
    show_list(ch);
  
    std::cout << "Press ENTER to quit." << std::flush;
    std::cin.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );
    return 0;
}
Topic archived. No new replies allowed.