Classes

I'm busy learning linked lists and was following a video tutorial since my text book doesn't make much sense to me. Anyway, I've copied all of the code from the video, but I cannot seem to compile it. I keep on getting the following error below relating to one of my classes. I've watched and re-watched the video, but I cannot seem to figure out where I went wrong - please help.

Error Message:
1
2
3
4
5
||=== ContactLinkedLists, Debug ===|
   In function 'std::ostream& operator<<(std::ostream&, const Contact&)':|
17|error: 'std::string Contact::name' is private|
12|error: within this context|
||=== Build finished: 2 errors, 0 warnings ===|


Header File Contact:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef CONTACT_H
#define CONTACT_H

#include <iostream>
#include <string>
using namespace std;

class Contact
{
    friend ostream& operator<<(ostream os, const Contact&c);
    friend class ContactList;

    public:
        Contact(string name="");
        ~Contact();
    private:
        string name;    //Error relating to this line 17
        Contact*next;
};

#endif  


Source file Contact:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "Contact.h"

#include <iostream>
#include <string>
using namespace std;

Contact::Contact(string n)
:name(n), next (NULL){
}

ostream& operator<<(ostream& os, const Contact&c){
    return os <<"Name: " <<c.name;               //Error relating to this line 12

}


Header file ContactList:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef CONTACTLIST_H
#define CONTACTLIST_H

#include "Contact.h"

#include <iostream>
#include <string>
using namespace std;


class ContactList
{
    public:
        ContactList();
        void addToHead(const string&);
        ~ContactList();

    private:
        Contact* head;
        int size;
};

#endif  


Source file ContactList:
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
 
#include "ContactList.h"

#include <iostream>
#include <string>
using namespace std;

ContactList::ContactList()
:head(NULL),size(0)
{
    //ctor
}

void ContactList::addToHead(const string& name){
    Contact* newOne = new Contact(name);

    if (head == NULL){
        head = newOne;
    }
    else{
        newOne ->next = head;
        head = newOne;
    }

    size++;
}

ContactList::~ContactList()
{
    //dtor
}


Source file Main Program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "ContactList.h"

#include <iostream>
#include <string>
using namespace std;

int main(){
    ContactList* cl1 = new ContactList();
    string name;

    while (true){
        cout <<"Enter the contact name (q to quit): ";
        cin >>name;

        if (name=="q")
            break;

        cl1->addToHead(name);
    }
    return 0;
}
What are you using to compile this?
I'm using codeblocks.
1
2
3
friend ostream& operator<<(ostream  os, const Contact&c);

       ostream& operator<<(ostream& os, const Contact&c)
Spot the difference
Thank you so much! Compiles perfectly now.

I still need to learn what type of errors to look for :) and even then, just to have a second (or third) pair of eyes going over the code helps a lot!
Topic archived. No new replies allowed.