Overloading the << Operator.

I'm struggling to get my code to compile, but I currently have only one error in my innermost class.

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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <iostream>

template <class T>
class ArrayList
{
private:
    T* data;
    unsigned int size;

public:
    void append(T d); //adds d at the end of the list
    void insert(T d, unsigned int p); //adds d at position p
    void remove(T d); //finds d and removes it from the list

    unsigned int getSize() const;
    void display(); //alternative to the overloaded << operator
    T operator[](const unsigned int& p) const;
    friend std::ostream& operator<<(std::ostream& s, const ArrayList<T>& a);
    void operator=(const ArrayList<T>& a);
    bool operator==(const ArrayList<T>& a); //behaves like stringcomp
    void operator+=(const ArrayList<T>& a);
    bool litComp(char c[]);

    ArrayList();
    ArrayList(const ArrayList& c); //copy-constructor
    ArrayList(unsigned int s); //used to pre-allocate space
    ~ArrayList();
};

template <class T>
void ArrayList<T>::append(T d)
{
    T* temp = new T[size + 1];

    for(int i = 0; i < size; i++)
        temp[i] = data[i];
    
    data[size] = d;

    data = temp;
    ++size;
    delete[] temp;
}

template <class T>
void ArrayList<T>::insert(T d, unsigned int p)
{
    T* temp = new T[size + 1];

    for(int i = 0; i < size; i++)
    {
        if(i = p)
            temp[i] = d;
        else
            temp[i] = data[i];
    }

    data = temp;
    ++size;
    delete[] temp;
}

template <class T>
void ArrayList<T>::remove(T d)
{
    T* temp = new T[size - 1];

    for(int i = 0; i < size; i++)
    {
	if(data[i] == d)
        ++i;
	else
        temp[i] = data[i];
    }

    data = temp;
    --size;
    delete[] temp;
}

template <class T>
unsigned int ArrayList<T>::getSize() const
{
    return size;
}

template <class T>
void ArrayList<T>::display() //My compiler tells me "<< is not a member of ArrayList<T>."
{
    for(int i = 0; i < size; i++)
        std::cout << data[i] << std::endl << std::endl;
}

template <class T>
T ArrayList<T>::operator[](const unsigned int& p) const
{
    return data[p];
}

template <class T>
void ArrayList<T>::operator=(const ArrayList<T>& a)
{
    data = new T[a.getSize()];
    size = a.getSize();

    for(int i = 0; i < size; i++)
	data[i] = a[i];
}

template <class T>
std::ostream& ArrayList<T>::operator<<(std::ostream& s, const ArrayList<T>& a)
{
    for(int i = 0; i < a.getSize(); i++)
        s << a[i];

    return s;
}

template <class T>
bool ArrayList<T>::operator==(const ArrayList<T>& a)
{
    int i = 0;
    bool equal = true;
    while(data[i] != '\0' || a[i] != '\0')
    {
        if(data[i] != a[i])
        equal = false;
    }
	
    return equal;
}

template <class T>
void ArrayList<T>::operator+=(const ArrayList<T>& a)
{
    for(int i = 0; i < a.getSize(); i++)
        append(a[i]);
}

template <class T>
bool ArrayList<T>::litComp(char c[])
{
    bool eq = true;
    for(int i = 0; (data[i] || c[i]) != '\0'; i++)
    {
        if(data[i] != c[i])
    eq = false;
    }
	
	return eq;
}

template <class T>
ArrayList<T>::ArrayList()
{
    data = new T;
    size = 0;
}
template <class T>
ArrayList<T>::ArrayList(const ArrayList<T>& c)
{
	*this = c;
}
template <class T>
ArrayList<T>::ArrayList(unsigned int s)
{
    data = new T[s];
    size = s;
}

template <class T>
ArrayList<T>::~ArrayList()
{
    if(data != NULL)
        delete[] data;
}


What else can you tell me?
Last edited on
The main problem I can see is that on line 111 you are saying that operator<< is a member of ArrayList, when it isn't, it's just a friend.

What are the errors that you are getting? That's a lot easier than just guessing based on the title of your post.
Thanks, that cleared the error right up.
New issue on the same subject, so I'm putting it here.

1
2
3
4
5
6
7
8
9
    friend std::ostream& operator<<(std::ostream& s, const Account& h)
    {
        s << "Login: " << h.getUser() << std::endl; //apparently this is trying to convert a const instance to a reference
        s << "Directory: " << h.getHome() << std::endl; //same with the other three
        s << "Shell: " << h.getShell() << std::endl;
        s << "Gecos: " << h.getGecos() << std::endl;

        return s;
    }


Will provide full file upon request
Why are the member and non-member overloaded operators returning void?! There's a lot wrong with this code: insert and append is totally incorrect, I don't know where to start the correction. For example, every call to append moves the existing elements without freeing up the old one, you then point
data
to
temp
, then
delete[]
it again.

A very naïve possible implementation can be found here https://github.com/iamOgunyinka/maths/blob/master/vector.h

NOTE: If I wasn't typing on my phone, I'd have loved to take my time to point the OP in the right direction -- one step at a time.
In answer to your question, @Sh0es, we probably need to see at least the details for your Account class. However, I can guess what is going on.

You are passing a const reference of your class. This means it is immutable, so you are not allowed to change it in any way. Most likely, your 'get' functions have not been made immutable, so you will need to change them. Here is what it should look like
1
2
3
4
5
6
7
8
9
10
11
12
13
class Account {
    public:
        std::string getUser() const { // note the const
            return this->_user; 
        } 

        // same for your other 'get' functions
        // ...

    private:
        std::string _user;
        // ...
};
Last edited on
Topic archived. No new replies allowed.