|
Problems compiling an example
Sorry for the length of the following code, but I'm at a loss as to how much is important. I first tried inputting this example by hand from my textbook, then got the source code from the author's web site, which is what I am enclosing in its entirety. I get the following error messages:
Quote:
basic_string.h:2354: note: std::basic_istream<_CharT, _Traits>& std: perator>>(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
basic_string.h:2408: note: candidates are: std::basic_istream<_CharT, _Traits>& std::getline(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&, _CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
entry.cpp:73: note: std::istream& operator>>(std::istream&, entry&)
entry.cpp:76: error: no matching function for call to 'getline(std::basic_istream<char, std::char_traits<char> >&, std::string, char)'
entry.cpp:78: error: no match for 'operator>>' in 'ins >> entry::getNumber() const()'
istream:131: note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>: perator>>(std::basic_istream<_CharT, _Traits>& (*)(std::basic_istream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
istream:134: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>: perator>>(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
istream:137: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>: perator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
istream:169: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>: perator>>(bool&) [with _CharT = char, _Traits = std::char_traits<char>]
istream:172: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>: perator>>(short int&) [with _CharT = char, _Traits = std::char_traits<char>]
istream:175: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>: perator>>(short unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
istream:178: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>: perator>>(int&) [with _CharT = char, _Traits = std::char_traits<char>]
istream:181: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>: perator>>(unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
istream:184: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>: perator>>(long int&) [with _CharT = char, _Traits = std::char_traits<char>]
istream:187: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>: perator>>(long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
istream:191: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>: perator>>(long long int&) [with _CharT = char, _Traits = std::char_traits<char>]
istream:194: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>: perator>>(long long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
istream:198: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>: perator>>(float&) [with _CharT = char, _Traits = std::char_traits<char>]
istream:201: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>: perator>>(double&) [with _CharT = char, _Traits = std::char_traits<char>]
istream:204: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>: perator>>(long double&) [with _CharT = char, _Traits = std::char_traits<char>]
istream:207: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>: perator>>(void*&) [with _CharT = char, _Traits = std::char_traits<char>]
istream:230: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>: perator>>(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]
|
Main:
// FILE: TelDirecMenu.cpp
// MENU DRIVEN TELEPHONE DIRECTORY UPDATE PROGRAM
#include "indexList.h"
#include "entry.h"
#include <cctype> // For toupper
#include <iostream>
#include <string>
using namespace std;
typedef indexList<entry, 100> telIndexList;
// Function prototype
// PERFORMS USER SELECTION
void select(telIndexList&, char);
int main()
{
telIndexList telDirec; // telephone directory
char choice; // menu choice
// Read the initial directory.
cout << "Enter the initial directory entries -" << endl;
telDirec.read(cin);
// Keep reading and performing operations until user enters Q
do
{
// Display the menu.
cout << "Enter your choice -" << endl;
cout << "A(Add), C(Change), D(Delete)," << endl
<< "G(Get), S(Sort), P(Print), Q(Quit): ";
cin >> choice;
cin.ignore(80, '\n'); // skip newline
// Perform the operation selected.
select(telDirec, choice);
}
while (toupper(choice) != 'Q');
return 0;
}
// Performs user selection
void select(telIndexList& telDirec, // INOUT : directory
char choice) // IN: selection
{
// Local data
entry anEntry; // one entry
string aName; // input - entry name
int index; // index of name in directory
char answer; // input - indicates whether to add
// entry for missing name
switch (toupper(choice))
{
case 'A': // Add an entry
cout << "Enter entry to add - ";
cin >> anEntry;
telDirec.append(anEntry);
break;
case 'C': // Change an entry
cout << "Enter entry to change - ";
cin >> anEntry;
index = telDirec.search(anEntry);
if (index >= 0)
telDirec.replace(index, anEntry);
else
{
cout << "Name not in directory. "
<< "Do you wish to add it (Y or N): ";
cin >> answer;
if (toupper(answer) == 'Y')
telDirec.append(anEntry);
}
break;
case 'D': // Delete an entry
cout << "Enter name of entry to delete: ";
//cin.ignore(1, '\n');
getline(cin, aName, '\n');
anEntry.setEntry(aName);
index = telDirec.search(anEntry);
if (index >= 0)
telDirec.remove(index);
else
cout << "Entry not found - no deletion" << endl;
break;
case 'G': // Get a number
cout << "Enter name of entry to get: ";
//cin.ignore(1, '\n');
getline(cin, aName, '\n');
anEntry.setEntry(aName);
index = telDirec.search(anEntry);
if (index >= 0)
{
telDirec.retrieve(index, anEntry);
cout << "The number you requested is "
<< anEntry.getNumber() << endl;
}
else
cout << "Not in directory." << endl;
break;
case 'S': // Sort directory
telDirec.selSort();
break;
case 'P': // Print directory
telDirec.display();
break;
case 'Q': // Quit directory
cout << "Exiting program" << endl;
break;
default:
cout << "Choice is invalid - try again" << endl;
//cin.ignore(80, '\n');
}
} // end select
Entry.h:
// File: entry.h
// Definition for a directory entry class
#include <iostream>
#include <string>
using namespace std;
#ifndef ENTRY_H
#define ENTRY_H
class entry
{
public:
// Member functions
// constructor
entry();
// Store data in an entry
void setEntry(const string&, const string& nr = "");
// accessor functions
string getName() const;
string getNumber() const;
// operators
//bool operator == (const entry&);
friend bool operator == (const entry&, const entry&);
bool operator < (const entry&);
bool operator > (const entry&);
// friends
friend ostream& operator << (ostream&, const entry&);
friend istream& operator >> (istream&, entry&);
private:
string name; // Person's name
string number; // and phone number
};
#endif // ENTRY_H
entry.cpp:
// File: entry.cpp
// Implementation file for class entry
#include "entry.h"
#include <iostream>
#include <string>
using namespace std;
// constructor
entry::entry()
{
name = "";
number = "";
}
// Store data in an entry
void entry::setEntry(const string& na, // IN: name
const string& nr) // IN: number
{
name = na;
number = nr;
}
// Get name
string entry::getName() const
{
return name;
}
// Get number
string entry::getNumber() const
{
return number;
}
// Operators
/*
bool entry::operator == (const entry& dE) // IN: right-operand
{
return (name == dE.name);
}
*/
bool operator == (const entry& dE1, const entry&dE) // IN: right-operand
{
return (dE1.getName() == dE.getName());
}
bool entry::operator < (const entry& dE) // IN: right-operand
{
return (name < dE.getName());
}
bool entry::operator > (const entry& dE) // IN: right-operand
{
return (name > dE.getName());
}
// friends
ostream& operator << (ostream& outs, // INOUT: stream
const entry& dE) // IN: entry displayed
{
outs << "Name is " << dE.getName() << endl;
outs << "Number is " << dE.getNumber() << endl;
return outs;
}
istream& operator >> (istream& ins, // INOUT: stream
entry& dE) // OUT: entry read
{
cout << "Enter name: ";
getline(ins, dE.getName(), '\n');
cout << "Enter number: ";
ins >> dE.getNumber();
return ins;
}
indexList.h:
// File: indexList.h
// Definition of indexed list template class
#ifndef INDEXLIST_H
#define INDEXLIST_H
#include <iostream>
using namespace std;
template <class T, int maxSize>
class indexList
{
public:
// Constructor
indexList();
// Add an item to the end of an indexed list
bool append(const T&); // IN: item appended
// Overwrite an element at a specified index
bool replace(int, // IN: insertion index
const T&); // IN: item inserted
// Insert an element at a specified index
bool insert(int, // IN: insertion index
const T&); // IN: item inserted
// Retrieve an element at a specified index
bool retrieve(int, // IN: index
T&) const; // OUT: value retrieved
// Delete an element at a specified index
bool remove(int); // IN: index
// Find index of smallest value in a sublist
int findMin(int, // IN: start index
int) const; // OUT: end index
// Find index of largest value in a sublist
int findMax(int, // IN: start index
int) const; // OUT: end index
// Find index of a target item
// Returns -1 if target item not found
int search(const T&) const; // IN: target item
// Sort an indexed list
void selSort();
// Read data into the list
void read(istream&);
// Display the list contents
void display() const;
// Get the current size
int getSize() const;
private:
T elements[maxSize]; // Storage for elements
int size; // Count of elements in list
};
#endif // INDEXLIST_H
indexList.cpp:
// File: indexList.cpp
// Indexed list class implementation
#include "indexList.h"
#include <iostream>
using namespace std;
template <class T, int maxSize>
indexList<T, maxSize>::indexList() // constructor
{
size = 0; // list is empty
}
// Add an item to the end of an indexed list
template <class T, int maxSize>
bool indexList<T, maxSize>::append(const T& item)
// Pre: item is defined
// Post: if size < maxSize, item is appended to list
// Returns: true if item was appended; otherwise, false
{
bool result;
// Add item to the end of the list if list is not full.
if (size < maxSize)
{
elements[size] = item;
size++;
result = true;
}
else
{
cerr << "Array is filled - can't append!" << endl;
result = false;
}
return result;
}
// Replace an item at a specified index with a new one.
// Pre: item and index are defined
// Post: item overwrites old item at position index if valid
// Returns: true if item was inserted; otherwise, false
template <class T, int maxSize>
bool indexList<T, maxSize>::replace(int index, const T& item)
{
bool result;
// Overwrite a list element if index is valid.
if (index >= 0 && index < size)
{
elements[index] = item;
result = true;
}
else
{
cerr << "Index " << index << " not in filled part"
<< " - can't insert!" << endl;
result = false;
}
return result;
}
template <class T, int maxSize>
bool indexList<T, maxSize>::insert(int index, const T& item)
{
bool result;
int i;
// Insert a list element if index is valid.
if (index >= 0 && index < size && size < maxSize)
{
// Shift elements down 1 position
for (i = size - 1; i >= index; i--)
elements[i + 1] = elements[i];
elements[index] = item; // insert
size++;
result = true;
}
else
{
cerr << "Index " << index << " not in filled part"
<< " - can't insert!" << endl;
result = false;
}
return result;
}
// Retrieve an item at a specified index
// Pre: item and index are defined
// Post: if index is valid, elements[index] is returned
// Returns: true if item was returned; otherwise, false
template <class T, int maxSize>
bool indexList<T, maxSize>::retrieve(int index, T& item) const
{
bool result;
// Return a list element through item if index is valid.
if (index >= 0 && index < size)
{
item = elements[index];
result = true;
}
else
{
cerr << "Index " << index << " not in filled part"
<< " - can't retrieve!" << endl;
result = false;
}
return result;
}
// Delete an element at a specified index
// Pre: index is defined
// Post: if index is valid, elements[index] is replaced
// with elements[size] and size is decremented.
// Returns: true if item was deleted; otherwise, false
template <class T, int maxSize>
bool indexList<T, maxSize>::remove(int index)
{
bool result;
int i;
// Delete element at index i by moving elements up
if (index >= 0 && index < size)
{
// Shift each element up 1 position
for (i = index + 1; i < size; i++)
elements[i-1] = elements[i];
size--; // Decrement size
result = true;
}
else
{
cerr << "Index " << index << " not in filled part"
<< " - can't delete!" << endl;
result = false;
}
return result;
}
/*
// Read data into the list
// Pre: none
// Post: All data items are stored in array elements
// and size is the count of items
template <class T, int maxSize>
void indexList<T, maxSize>::read(istream& ins)
{
T nextItem; // input - next data item
size = 0; // The list is empty.
ins >> nextItem;
while (!ins.eof() && size < maxSize)
{
elements[size] = nextItem;
size++;
cout << "Enter next item - ";
ins >> nextItem;
}
}
*/
// Read data into the list
// Pre: none
// Post: All data items are stored in array elements
// and size is the count of items
template <class T, int maxSize>
void indexList<T, maxSize>::read(istream& ins)
{
int numItems; // input - number of items to read
T nextItem; // input - next data item
cout << "Enter number of list items to read: ";
ins >> numItems;
ins.ignore(80, '\n'); // skip newline
// If numItems is valid, read each list element,
// starting with first element.
size = 0; // The list is empty.
if (numItems >= 0 && numItems <= maxSize)
while (size < numItems)
{
cout << "Enter next item - ";
ins >> nextItem;
elements[size] = nextItem;
size++;
}
else
cerr << "Number of items " << numItems
<< " is invalid"
<< " - data entry is cancelled!" << endl;
}
// Display the list contents
// Pre: none
// Post: Displays each item stored in the list
template <class T, int maxSize>
void indexList<T, maxSize>::display() const
{
// Display each list element.
for (int i = 0; i < size; i++)
cout << elements[i] << endl;
}
// Find index of a target item
// Pre: none
// Post: Returns the index of target if found;
// otherwise, return -1.
template <class T, int maxSize>
int indexList<T, maxSize>::search(const T& target) const
{
for (int i = 0; i < maxSize; i++)
if (elements[i] == target)
return i; // target found at position i
// target not found
return -1;
}
// Get the current size
template <class T, int maxSize>
int indexList<T, maxSize>::getSize() const
{
return size;
}
// Sort the indexed list
template <class T, int maxSize>
void indexList<T, maxSize>::selSort()
{
// Selection sort stub - do nothing
}
TIA to anyone who can help me sort this out.
|