The coding is exactly as it is in the book but I get the following error:
mingw32-g++.exe -c listAgainToday.cpp -o listAgainToday.o
listAgainToday.cpp:15:11: error: declaration of 'class T'
listAgainToday.cpp:12:10: error: shadows template parm 'class T'
listAgainToday.cpp:16:8: error: 'ostream' does not name a type
Process terminated with status 1 (0 minute(s), 0 second(s))
3 error(s), 0 warning(s) (0 minute(s), 0 second(s))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
//#using namespace std;
template<typename T>
struct node
{
T info;
node<T> *next;
node<T> *back;
};
template<typename T>
class list {
template <typename T>
friend ostream& operator<<(ostream&,
const list<T>&);
//Overload the stream insertion operator
Thank you very much Repeater I am very much frustrated by this problem but now I am getting this error:
mingw32-g++.exe -c listAgainToday.cpp -o listAgainToday.o
listAgainToday.cpp:64:41: error: 'std::ostream& list<T>::operator<<(std::ostream&, const list<T2>&)' must take exactly one argument
Process terminated with status 1 (0 minute(s), 0 second(s))
1 error(s), 0 warning(s) (0 minute(s), 0 second(s))
I have been checking and checking I do not get the problem
#include <iostream>
usingnamespace std;
template<typename T>
struct node
{
T info;
node<T> *next;
node<T> *back;
};
template<typename T>
class list {
template <typename T2>
friend ostream& operator<<(ostream&, const list<T2>&);
//Overload the stream insertion operator
public:
list(); // constructor
list(const list &l); // copy constructor
list &operator=(const list &l); // assignment operator
//list &operator<<(const list &l);
~list(); // destructor
// Returns number of elements in the list
unsigned size();
// Returns true if the list is empty, false otherwise.
bool isEmpty() const;
// Inserts element to front of list
void insertFront(const T &info);
// Inserts element to the end of list
void insertBack(const T &info);
// Returns the values of the front element in the list
T front();
// Returns the value of the back element of the list.
T back();
// Deletes the front element of the list and returns its value
void removeFront();
// Deletes the back element of the list and returns its value
void removeBack();
// Prints each element of the list in order
void printForward();
// Prints each element of the list in reverse order
void printReverse();
private:
struct node {
node *next;
node *prev;
T info;
};
template <typename T2>
ostream& operator<<(ostream& osObject, const list<T2>& list )
{
node<T> *current;
current = list.first;
while(current != NULL)
{
osObject<< current->info <<" ";
current = current->next;
}
return osObject;
}
node *first; // The pointer to the first node
node *last; // The pointer to the last node
unsigned length; // holds number of elements in the list
// Initializes empty list
void createEmpty();
// Removes all of the elements in the list
void removeAll();
// Makes copy of all of the elements in the list
void copyAll(const list &l);
};
template <typename T>
void list<T>::createEmpty() {
first = NULL;
last = NULL;
length = 0;
}
template <typename T>
void list<T>::removeAll() {
while (!isEmpty()) {
removeFront();
}
}
template <typename T>
void list<T>::copyAll(const list &l) {
node *iterator = l.first;
while (iterator) {
T obj = iterator->info;
insertBack(obj);
iterator = iterator->next;
}
}
template <typename T>
unsigned list<T>::size() {
return length;
}
template <typename T>
bool list<T>::isEmpty() const{
return (first == NULL && last == NULL);
}
template <typename T>
void list<T>::insertFront(const T &info) {
length++;
node *newNode = new node;
new (&(newNode->info)) T(info);
newNode->next = first;
newNode->prev = NULL;
if (isEmpty()) first = last = newNode;
else {
first->prev = newNode;
first = newNode;
}
}
template <typename T>
void list<T>::insertBack(const T &info) {
length++;
node *newNode = new node;
new (&(newNode->info)) T(info);
newNode->next = NULL;
newNode->prev = last;
if (isEmpty()) first = last = newNode;
else {
last->next = newNode;
last = newNode;
}
}
template <typename T>
T list<T>::front() {
return first->info;
}
template <typename T>
T list<T>::back() {
return last->info;
}
template <typename T>
void list<T>::removeFront() {
if (isEmpty()) return;
length--;
node *removedNode = first;
first = first->next;
if (first) first->prev = NULL;
else first = last = NULL;
delete removedNode;
return;
}
template <typename T>
void list<T>::removeBack() {
if (isEmpty()) return;
length--;
node *removedNode = last;
last = last->prev;
if (last) last->next = NULL;
else first = last = NULL;
delete removedNode;
return;
}
template <typename T>
list<T>::list() {
createEmpty();
}
template <typename T>
list<T>::list(const list &l) {
createEmpty();
copyAll(l);
return;
}
template <typename T>
list<T>& list<T>::operator= (const list &l)
{
if (this != &l) {
removeAll();
copyAll(l);
}
return *this;
}
template <typename T>
list<T>::~list() {
removeAll();
}
template <typename T>
void list<T>::printForward() {
if (isEmpty()) {
std::cout << "List is empty\n";
return;
}
node *head = first;
while (head) {
std::cout << head->info << " ";
head = head->next;
}
std::cout << "\n";
}
template <typename T>
void list<T>::printReverse() {
if (isEmpty()) {
std::cout << "List is empty\n";
return;
}
node *tail = last;
while (tail) {
std::cout << tail->info << " ";
tail = tail->prev;
}
std::cout << "\n";
}
#include <iostream>
usingnamespace std;
int main()
{
list<int> list1, list2;
int num;
cout<<"Enter integers ending with -999"<<endl;
cin>> num;
while(num != -999)
{
list1.insertFront(num);
cin >> num;
}
cout<<endl;
cout<<"List 1: " <<list1 << endl;
list2 = list1; //test the assignment operator;
cout <<"List 2: " << list2 <<endl;
cout<<"Enter the number to be deleted: ";
cin>> num;
cout<< endl;
list2.removeFront();
cout<<"After deleting the node, "
<<"List 2: "<<endl <<list2
<<endl;
return 0;
}
The problem seems to be at line 63:41 on the definition of operator<<