Linked Lists Help.

I am trying to have a print function and there is just something I am not getting with my client code. It seems all my other files are fine. Frankly I have been going at this for several hours now and I probably just burned out, but I would like get this to work before I call it day. Here are my errors and code.

LinkedLists\main.cpp|3|error: ContactList.h: No such file or directory|
LinkedLists\main.cpp|4|error: Contact.h: No such file or directory|
LinkedLists\main.cpp||In function 'int main()':|
LinkedLists\main.cpp|10|error: 'ContactList' was not declared in this scope|
LinkedLists\main.cpp|10|error: 'cl1' was not declared in this scope|
LinkedLists\main.cpp|10|error: expected type-specifier before 'ContactList'|
LinkedLists\main.cpp|10|error: expected ';' before 'ContactList'|
||=== Build finished: 6 errors, 0 warnings ===|


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef CONTACT_H_INCLUDED
#define CONTACT_H_INCLUDED

#include <iostream>
#include <string>

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

    public:
        Contact(std::string name = "none");

    private:
        std::string name;
        Contact* next;
};

#endif // CONTACT_H_INCLUDED 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef CONTACTLIST_H_INCLUDED
#define CONTACTLIST_H_INCLUDED

#include <iostream>
#include <string>
#include "Contact.h"

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

    private:
        Contact* head;
        int size;
};

#endif // CONTACTLIST_H_INCLUDED 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include "Contact.h"

using namespace std;


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

}

ostream& operator<<(ostream& os,const Contact& c)
{
    return os <<"Name: "<< c.name;
}


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
#include <iostream>
#include <string>
#include "ContactList.h"

using namespace std;


ContactList::ContactList():head(NULL),size(0)
{

}

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

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


void ContactList::printList()
{
    Contact* tp = head;

    while(tp != NULL)
    {
        cout << *tp << endl;
        tp = tp->next;
    }

}



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
#include <iostream>
#include <string>
#include "ContactList.h"
#include "Contact.h"

using namespace std;

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

    string name;

    while(true)
    {
        cout<< "Enter the name of the contact or q to quit." << endl;
        cin >> name;
        if(name == "q")
            break;
        cl1->addToHead(name);
    }

    cl1->printList();
}
This is probably the most important of your error messages and it tells you exactly what is wrong.
LinkedLists\main.cpp|3|error: ContactList.h: No such file or directory|
Where is your ContactList.h file located, the compiler can't find it in the normal places.

The same with this one:
LinkedLists\main.cpp|4|error: Contact.h: No such file or directory|

Are they located in the same directory as the file containing your main() function?

They are all located on my desktop in folder called linked lists. I am using code blocks add I added the files recursively. Should I have done it a different way?
I'm not a Windows user, but why did you place these files on you desktop instead of the directory where your project was created?

well what I did was i created each file first. Then I created a project and add the files to the project.
Despite "adding" the projects with Code::Blocks add files function, the source and header files themselves should still be in the same folder. I had the same problem once. :\
Yeah that was the problem thanks guys. Just sometimes these programs lol
Topic archived. No new replies allowed.