Linked Lists

So my teacher gave us a sample code and it works just fine when runs the program however whenever I try to run it I have errors and he is not to sure as to why I get them. I have uninstalled my compiler and reinstalled it and I am using the same as his. The code is identical. Here are the errors I get with the sample code. Let me know if you need more info.
Desktop\Project 2\Contact.cpp||In function 'int main()':|
Desktop\Project 2\Contact.cpp|10|error: expected primary-expression before '(' token|
Desktop\Project 2\Contact.cpp|10|error: expected primary-expression before 'n'|
Desktop\Project 2\Contact.cpp|10|error: expected ';' before ':' token|
Desktop\Project 2\Contact.cpp|16|error: a function-definition is not allowed here before '{' token|
||=== Build finished: 4 errors, 0 warnings ===|

Here is the Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include "Contact.h"

using namespace std;

int main()
{

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

}

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

}


You are trying to implement functions within a function, that is not allowed. Try:

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

using namespace std;

int main()
{

}

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

}

ostream& operator<<(ostream& os,const Contact& c)
{
    return os <<"Name: "<< c.name;
}
You can't define functions inside of main. Put those outside of main and then actually put code in main. Right now you have nothing that will actually do anything.
Awesome that error got fixed. ran into new ones. Here we are.

Desktop\Project 2\ContactList.cpp|5|error: expected unqualified-id before 'using'|
Desktop\Project 2\ContactList.cpp|15|error: ISO C++ forbids declaration of 'string' with no type|
Desktop\Project 2\ContactList.cpp|15|error: expected ',' or '...' before '&' token|
Desktop\Project 2\ContactList.cpp|15|error: prototype for 'void ContactList::addToHead(int)' does not match any in class 'ContactList'|
Desktop\Project 2\ContactList.h|12|error: candidate is: void ContactList::addToHead(const std::string&)|
||=== Build finished: 5 errors, 0 warnings ===|

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

using namespace std;

int main()
{
}

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++;
}



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

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

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

    private:
        Contact* head;
        int size;
}

#endif // CONTACTLIST_H_INCLUDED 
Last edited on
Forgot the ; in the class. I fixed that lol got the code to run thanks everyone!
Topic archived. No new replies allowed.