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();
}
|