Linked Lists, how do I implement a composed class as data for a node in an ordered linked list.

This is an assignment due in 6 days. The point is to make an address book to store the information of a contact. The information stored will be:

1.first and last name
2.relation to you ( a character{F,B or R)
3.address
4.phone number
5.birth date

The professor wants us to create an ordered linked list. I don't know how to start. I have created a composed class (extPersonType) that contains all the information needed to create a contact (the 5 above).

I was thinking about declaring extPersonType in the node as "info" or data contained in the node. So the input required of extPersonType goes to the info or data variable in the node.

In my program I haven't really declared a listType. I want it to be named addressBookType and make that the linked list. The addressBookType ordered linked list has to be able to:

1.)load data from file
2.)sort by last name ( I will be sorting as I build (insert or delete) data since it's a sorted linked list.)
3.)search by last name
4.)print by relation {F, B, R} (family, business associate, friend) (stored in relation from extPersonType)
5.)Print By month of birthDate
6.)Print all the people with the same last name
7.)add or delete contacts
8.)save data to file

Any suggestions on how I can create code for a sorted linked list named addressBookType using extPersonType as data for the nodes? I want it to be in separate files (.h & .cpp) I just am not quite sure on how to do this since I haven't dealt with linked lists before this assignment.

NOTE: I have to make a linkedListType first and then make sortedLinkedListType acccording to the professor. {I want to name it addressBookType}

I have provided code of my classes. Again... extPersonType is the class I want to implement in the node as data. Any suggestions/critics/advice are greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#pragma once
#include <string>
#include <iostream>


using namespace std;

class personType
{
public:
	personType(string = "", string = "");
	string getFirstName() const;
	string getLastName() const;
private:
	string firstName;
	string lastName;
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "personType.h"
personType::personType(string tFirst, string tLast)
{
	if (tFirst == "" && tLast == "")
	{
		cout << "invalid first and last name." << endl;
	}
	else
	{
		firstName = tFirst;
		lastName = tLast;
	}
}
string personType::getFirstName() const
{
	return firstName;
}
string personType::getLastName() const
{
	return lastName;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#pragma once
#include "iostream"
#include "string"

using namespace std;
class dateType
{
public:
	dateType(int month = 1, int day = 1, int year = 1);
	int getMonth() const;
	int getDay() const;
	int getYear() const;

private:
	int dDay;
	int dMonth;
	int dYear;
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "dateType.h"
dateType::dateType(int month, int day, int year)
{
	//sets and validates date
}
int dateType::getMonth() const
{
	return dMonth;
}
int dateType::getDay() const
{
	return dDay;
}
int dateType::getYear() const
{
	return dYear;
}

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

using namespace std;

class addressType
{
public:
	addressType(string = "", string = "", string = "", string = "");
	string getStreet() const;
	string getCity() const;
	string getState() const;
	string getZip() const;

private:
	string streetAddress;
	string cityAddress;
	string stateAddress;
	string zipAddress;
};

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
#include "addressType.h"


addressType::addressType(string street, string city, string state, string zip)
{
	if (street == "" && city == "" && state == "" && zip == "")
	{
		//exception, address is empty.
	}
	else
	{
		streetAddress = street;
		cityAddress = city;
		stateAddress = state;
		zipAddress = zip;
	}
}
string addressType::getStreet() const
{
	return streetAddress;
}
string addressType::getCity() const
{
	return cityAddress;
}
string addressType::getState() const
{
	return stateAddress;
}
string addressType::getZip() const
{
	return zipAddress;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#pragma once
#include "string"
#include "iostream"


using namespace std;

class phoneNumber
{
public:
	phoneNumber(string = "");
	string getPhone() const;

private:
	string formatedPhoneNum;
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "phoneNumber.h"
#include "string"
#include "iostream"

using namespace std;

phoneNumber::phoneNumber(string prototypePhoneNum)
{
//sets and validates phone number
}
string phoneNumber::getPhone() const
{
	return formatedPhoneNum;
}


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
#pragma once
#include "personType.h"
#include "dateType.h"
#include "AddressType.h"
#include "phoneNumber.h"

#include "string"
#include "iostream"

using namespace std;

class extPersonType
{
public:
	extPersonType(string = "", string = "", string = "", int = 1, int = 1, int = 1900, string = "", string = "", string = "", string = "", string = "");
	string getLastName() const;
	string getRelation() const;
	int getMonth() const;
	
	void print() const;
private:
	personType firstLastName;
	string relation;
	dateType dateOfBirth;
	addressType contactAddress;
	phoneNumber contactNumber;
};

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

#include "extPersonType.h"

extPersonType::extPersonType(string first, string last, string relat,
	int month, int day, int year, string street, string city,
	string state, string zip, string phone)
	:firstLastName(first, last), dateOfBirth(month, day, year),
	contactAddress(street, city, state, zip), contactNumber(phone)
{
	if (relat == "B" || relat == "F" || relat == "R")
	{
		relation = relat;
	}
	else
	{
		cout << "invalid relation" << endl; // exception, invalid relation.
	}

}

string extPersonType::getLastName() const
{
	return firstLastName.getLastName();
}

string extPersonType::getRelation() const
{
	return relation;
}

int extPersonType::getMonth() const
{
	return dateOfBirth.getMonth();
}

void extPersonType::print() const
{
//print function 
}


~thank you for your time.
> Any suggestions on how I can create code for a sorted linked list
> I have to make a linkedListType first and then make sortedLinkedListType acccording to the professor.

The suggestion given by your professor is a good one; keep separate concerns separate.

First implement a normal linked list, which does not concern itself with maintaining a sorted order.
Then, use this list to implement a sorted linked list of persons.

For example, using std::list<> as the core list implementation, creating a sorted list of persons using it would be quite straight-forward:

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
struct person
{
    std::string first_name ;
    std::string last_name ;
    // ...

    struct less // compare persons based on last_name, then first_name
    {
        static std::pair<std::string,std::string> name_pair( const person& per )
        { return std::make_pair( per.last_name, per.first_name ) ; }

        bool operator() ( const person& a, const person& b ) const
        { return name_pair(a) < name_pair(b) ; }
    };
};

struct ordered_list // list of person, ordered on last_name, then first_name
{
    void insert( const person& per )
    {
        // locate the correct position in the ordered list
        const auto iter = std::upper_bound( list.begin(), list.end(), per, person::less{} ) ;
        list.insert( iter, per ) ; // insert at that position
    }

    std::list<person> list ; // the actual list
};

http://coliru.stacked-crooked.com/a/488f78be9cd356d1
Can you please explain to me how I can put extPersonType as a dataType in an ordered list called addressBookType.

Your code seems to be cool for a list that only contains first and last names but if you read my post you will realize that there is more to it.

Thank you. But you kinda took half of what I asked and treated it like it was what I asked. I would appreciate it a lot if you answered my actual questions.
This is my ordered list code

//orderedLinkedList.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "linkedListType.h"
#pragma once

using namespace std;

template <class Type>
class orderedLinkedList :
	public linkedListType<Type>
{
public:
	bool search(const Type&) const;
	void insert(const Type&);
	void insertFirst(const Type&);
	void insertLast(const Type&);
	void deleteNode(const Type&);
};

//orderedLinkedList.cpp
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
#include "orderedLinkedList.h"


template <class Type>
bool orderedLinkedList<Type>::search(const Type& item) const
{
	bool found = false;
	nodeType<Type> *current;
	current = first;
	while (current != NULL && !found)
	{
		if (current->info >= item)
		{
			found = true;
		}
		else
		{
			current = current->link;
		}
	}
	if (found)
	{
		found = (current->info == item);
	}
	return found;
}

template <class Type>
void orderedLinkedList<Type>::insert(const Type& item)
{
	nodeType<Type> *current, *trailCurrent, *newNode;
	bool found;
	newNode = new nodeType<Type>;
	newNode->info = item;
	newNode->link = NULL;
	if (first == NULL)
	{
		first = newNode;
		last = newNode;
		count++;
	}
	else
	{
		current = first;
		found = false;
		while (current != NULL && !found)
		{
			if (current->info >= item)
			{
				found = true;
			}
			else
			{
				trailCurrent = current;
				current = current->link;
			}
		}
		if (current == first)
		{
			newNode->link = first;
			first = newNode;
			count++;
		}
		else
		{
			trailCurrent->link = newNode;
			newNode->link = current;
			if (current == NULL)
				last = newNode;
			count++;
		}
	}
}

template <class Type>
void orderedLinkedList<Type>::insertFirst(const Type& item)
{
	insert(item);
}

template <class Type>
void orderedLinkedList<Type>::insertLast(const Type& item)
{
	insert(item);
}

template <class Type>
void orderedLinkedList<Type>::deleteNode(const Type& item)
{
	nodeType<Type> *current, *trailCurrent;
	bool found;
	if (first == NULL)
	{
		cout << "The list is empty..." << endl;
	}
	else
	{
		current = first;
		found = false;
		while (current != NULL && !found)
		{
			if (current->info >= item)
			{
				found = true;
			}
			else
			{
				trailCurrent = current;
				current = current->link;
			}
		}
		if (current == NULL)
		{
			cout << "The item you are looking to delete is not in the list." << endl;
		}
		else
		{
			if (current->info == item)
			{
				if (first == current)
				{
					first = first->link;
					if (first == NULL)
						last = NULL;
					delete current;
				}
				else
				{
					trailCurrent->link = current->link;
					if (current == last)
						last = trailCurrent;
					delete current;
				}
				count--;
			}
			else
			{
				cout << "The item you are looking to delete is not in the list." << endl;
			}
		}
	}
}


I am trying to insert an extPersonType object into the ordered list in main

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

using namespace std;

int main()
{

	extPersonType entry("first", "last", "B",
		2, 12, 1990, "street", "city",
		"state", "zip", "7873334444");

	orderedLinkedList<extPersonType> AddressBook;

	AddressBook.insert(entry);

	system("pause");
	return 0;
}


I get these errors:

Error LNK2001 unresolved external symbol "public: virtual bool __thiscall orderedLinkedList<class extPersonType>::search(class extPersonType const &)const " (?search@?$orderedLinkedList@VextPersonType@@@@UBE_NABVextPersonType@@@Z)

Error LNK2001 unresolved external symbol "public: virtual void __thiscall orderedLinkedList<class extPersonType>::deleteNode(class extPersonType const &)" (?deleteNode@?$orderedLinkedList@VextPersonType@@@@UAEXABVextPersonType@@@Z)

Error LNK2001 unresolved external symbol "public: virtual void __thiscall orderedLinkedList<class extPersonType>::insertFirst(class extPersonType const &)" (?insertFirst@?$orderedLinkedList@VextPersonType@@@@UAEXABVextPersonType@@@Z)

Error LNK2001 unresolved external symbol "public: virtual void __thiscall orderedLinkedList<class extPersonType>::insertLast(class extPersonType const &)" (?insertLast@?$orderedLinkedList@VextPersonType@@@@UAEXABVextPersonType@@@Z)

Error LNK2019 unresolved external symbol "public: void __thiscall orderedLinkedList<class extPersonType>::insert(class extPersonType const &)" (?insert@?$orderedLinkedList@VextPersonType@@@@QAEXABVextPersonType@@@Z) referenced in function _main


Error LNK2019 unresolved external symbol "public: __thiscall linkedListType<class extPersonType>::linkedListType<class extPersonType>(void)" (??0?$linkedListType@VextPersonType@@@@QAE@XZ) referenced in function "public: __thiscall orderedLinkedList<class extPersonType>::orderedLinkedList<class extPersonType>(void)" (??0?$orderedLinkedList@VextPersonType@@@@QAE@XZ)

Error LNK2019 unresolved external symbol "public: __thiscall linkedListType<class extPersonType>::~linkedListType<class extPersonType>(void)" (??1?$linkedListType@VextPersonType@@@@QAE@XZ) referenced in function "public: __thiscall orderedLinkedList<class extPersonType>::~orderedLinkedList<class extPersonType>(void)" (??1?$orderedLinkedList@VextPersonType@@@@QAE@XZ)

What am I doing wrong? How can I fix this?

Move the definitions of the member functions from the cpp file to the header file

See: https://isocpp.org/wiki/faq/templates#templates-defn-vs-decl
I'm getting some errors that I think have to do with comparing objects of extPersonType. Can someone suggest how to overload the operators >= and == for extPersonType. The compiler seems to be confused (me too) as to what to do with comparison between these Types.
What do I want to compare? Well... last names. We can start there.
(I have the same code but I moved it into the .h file)
Error C2784 'bool std::operator >=(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &) noexcept': could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'extPersonType'

Error C2784 'bool std::operator >=(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &) noexcept': could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'extPersonType'

Error C2784 'bool std::operator >=(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)': could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'extPersonType'

Error C2784 'bool std::operator >=(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)': could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'extPersonType'

Error C2784 'bool std::operator >=(const std::move_iterator<_RanIt> &,const std::move_iterator<_RanIt2> &)': could not deduce template argument for 'const std::move_iterator<_RanIt> &' from 'extPersonType'

Error C2784 'bool std::operator >=(const std::move_iterator<_RanIt> &,const std::move_iterator<_RanIt2> &)': could not deduce template argument for 'const std::move_iterator<_RanIt> &' from 'extPersonType'

Error C2784 'bool std::operator >=(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)': could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'extPersonType'

Error C2784 'bool std::operator >=(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)': could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'extPersonType'

Error C2784 'bool std::operator >=(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)': could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'extPersonType'

Error C2784 'bool std::operator >=(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)': could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'extPersonType'

Error C2784 'bool std::operator >=(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)': could not deduce template argument for 'const _Elem *' from 'extPersonType'

Error C2784 'bool std::operator >=(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)': could not deduce template argument for 'const _Elem *' from 'extPersonType'

Error C2678 binary '==': no operator found which takes a left-hand operand of type 'extPersonType' (or there is no acceptable conversion)

Error C2678 binary '==': no operator found which takes a left-hand operand of type 'extPersonType' (or there is no acceptable conversion)

Error C2676 binary '>=': 'extPersonType' does not define this operator or a conversion to a type acceptable to the predefined operator

Error C2676 binary '>=': 'extPersonType' does not define this operator or a conversion to a type acceptable to the predefined operator












OK I tried overloading and some errors went away but new errors arrived.

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
const bool& extPersonType::operator==(const extPersonType& other) //ERROR C3867
{
	if (this->getLastName == other.getLastName)
	{
		return true;
	}
	else
	{
		return false;
	}
}

const bool& extPersonType::operator>=(const extPersonType& other)
{
	string name1 = "";
	string name2 = "";

	name1 = this->getLastName; //ERROR C3867 and C2679
	name2 = other.getLastName; //ERROR C3867 and C2679

	if (name1.compare(name2) == 0)
	{
		return true;
	}
	else if (name1.compare(name2) > 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}


Error C3867 'extPersonType::getLastName': non-standard syntax; use '&' to create a pointer to member

Error C2679 binary '=': no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)


Last edited on
1
2
3
4
5
6
7
8
9
10
11
// const bool& extPersonType::operator==(const extPersonType& other) 
bool extPersonType::operator== ( const extPersonType& other ) const // const-correct
{
	// if (this->getLastName == other.getLastName)
        return this->getLastName() == other.getLastName() ; // () to call the function
}

bool extPersonType::operator>= ( const extPersonType& other ) const // const-correct
{
        return this->getLastName() >= other.getLastName() ;
}


Note: return the bool result by value; getLastName() should also be const-correct
ie. extPersonType::getLastName() const
Last edited on
Awesome!! Thanks JLBorges.

I have fixed the issue with the == >= overloads and the program worked for a second... I actually inserted my first object into the ordered linked list "AddressBook" and managed to print it with that one object! I'm still working on completing the program..


Last edited on
Topic archived. No new replies allowed.