Help with code

Hi. I´m new to C++, but I already worked with C, and I´m having some trouble to work with OO together with pointers. The code below is aimed to manage pile it´s compiling but it´s not working. Any help would be deeply appreciated. Thank yoou very much.

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
#include <stdio.h>
#include <string.h>
#include <cstdlib>
#include <iostream>

using namespace std;

class ListItem
    {
    public:
        ListItem (char *);
        ListItem *next;
        char word [];
    };

ListItem::ListItem(char *wordParam)
    {
    next = NULL;
    strcpy(wordParam, word);
    }

class List
    {
    public:
        List();
        int length;
        ListItem *firstItemRef;
        void Add(ListItem *listItemParam);
//        void InsertFirst(ListItem listItemParam);
//        void InsertLast(ListItem listItemParam);
//        void Remove(ListItem listItemParam);
//        void RemoveFirst();
//        void RemoveLast();
//        void RemoveIndex(int indexParam);
//        void Dispose();
    };

List::List()
    {
    length = 0;
    firstItemRef = NULL;
    }

void List::Add(ListItem *listItemParam)
    {
    length = 0;
    printf("%d", &firstItemRef);
   
    if (firstItemRef != NULL)
        {
        ListItem *listItem = firstItemRef;
       
        while(listItem->next != NULL)
            {
            length++;
            listItem = listItem->next;
            }
           
        listItem->next = listItemParam;
        }
    else
        {
        length = 1;
        firstItemRef = listItemParam;
        }
    }

int main(int argc, char *argv[])
    {
    List *list = new List();
    ListItem *item = new ListItem("firstItem");
    list->Add(item);
    
    printf("hello friends");
    scanf("\n");
    }
Last edited on
You can't have arrays of undefined length (line 13). Either choose a number, use char* and allocate memory of use std::string.
First argument of strcpy is destination (line 19), so you're trying to write on "firstItem" (which by the way is a const char*).
Other than that it seems ok..
Topic archived. No new replies allowed.