I have a doubly linked list telephone directory programme. I have a function that makes the list whilst also making links with pointers to other structures as I go along. When I try to refer to them I get the errors detailed below.
#include <iostream>
#include <iomanip>
#include <string>
usingnamespace std;
constint MAXRECS = 3; // maximum number of records
struct TeleType
{
string name;
string phoneNo;
TeleType *nextaddr;
TeleType *prevaddr;
};
void populate(TeleType *); // function prototype needed by main()
void display(TeleType *); // function prototype needed by main()
void remove(TeleType *); //function prototype for main()
void make_list();
TeleType *head, *current, *tail; // 3 pointers to structures of
// type TeleType
// get a pointer to the first structure in the list
TeleType head, tail, current;
int main()
{
cout << "The list will now be made" << endl;
make_list();
cout << "\nThe list consists of the following records:\n";
display(head); // display the structures
system("pause");
return 0;
}
void make_list()
{
//Input values for head structure
populate(head);
//Assign nextaddr pointer to current
head->nextaddr = current;
//Input values for current structure
populate(current);
//Assign current structure's newaddr and preaddr
current->prevaddr = head;
current->nextaddr = tail;
//Input values for tail
populate(tail);
//Assign prevaddr pointer
tail->prevaddr = current;
//assign newaddr pointer as null to tail structure
tail->nextaddr = NULL;
// assign prevaddr pointer as current pointer for tail structure
return;
}
// input a name and phone number
void populate(TeleType *record) // record is a pointer to a
{ // structure of type TeleType
cout << "Enter a name: ";
getline(cin, record->name);
cout << "Enter the phone number: ";
getline(cin, record->phoneNo);
return;
}
void display(TeleType *contents) // contents is a pointer to a
{ // structure of type TeleType
while (contents != NULL) // display until end of linked list
{
cout << endl << setiosflags(ios::left)
<< setw(30) << contents->name
<< setw(20) << contents->phoneNo
<< setw(20) << contents->prevaddr
<< setw(20) << contents->nextaddr;
contents = contents->nextaddr;
}
cout << endl;
return;
}
1>------ Build started: Project: Exercise13.5_2, Configuration: Debug Win32 ------
'head': 'TeleType' differs in levels of indirection from 'TeleType *'
1>c:\users\kish\google drive\c++
error C2040: 'tail': 'TeleType' differs in levels of indirection from 'TeleType *'
error C2040: 'current': 'TeleType' differs in levels of indirection from 'TeleType *'
1>Done building project "Exercise13.5_2.vcxproj" -- FAILED.
TeleType *head, *current, *tail; // 3 pointers to structures of
// type TeleType
// get a pointer to the first structure in the list
TeleType head, tail, current;
You are trying to define two different variables, both called head, or current, or tail. You must have different identifiers. Rename one of the sets.
Once you do this, you'll also need to make sure the pointer to the, for example, head, actually points to the head object. TeleType* phead = &head;
I also suggest not getting in the habit of using global variables. It'll bite you later on when you have more complex programs.