Memo
09-18-2002, 07:04 PM
So I had been taking all the theoretical computer science classes and now i'm back into c++. I'm supposed to write a template based list and I've been really budy lately and didn't have time. Prof said it's fine to copy the code out of the book for first assignment so I did so. But I can't get it to compile. here is the code I have:
// ----------
// hw1.cpp
// Memo Sanchez
// First Homework Assignment
// ----------
#include <cassert> // assert
#include <cstddef> // size_t
#include <cstring> // strlen
#include <ctime> // CLOCKS_PER_SEC, clock_t, time_t, clock, ctime, time
#include <iostream> // cout, <<, endl
#include <fstream> // ifstream, ofstream
#include <cmath>
#include <algorithm> // copy, max_element, min_element, sort, transform
#include <functional> // bind2nd, less, minus, multiplies, mem_fun_ref
#include <iterator> // back_inserter, istream_iterator, ostream_iterator
#include <memory> // allocator<T>, uninitialized_copy, uninitialized_fill
#include <numeric> // accumulate
#include <stdexcept> // out_of_range
using namespace std;
template <class Object>
class List
{
public:
List()
{
header = new ListNode<Object>;
}
List(const List &rhs)
{
header = new ListNode<Object>;
*this = rhs;
}
~List()
{
makeEmpty();
delete header;
}
bool isEmpty() const
{
return header->next == NULL;
}
void makeEmpty()
{
while(!isEmpty())
remove(first().retrieve());
}
ListItr<Object> zeroth()const
{
return ListItr<Object>(header);
}
ListItr<Object> first()const
{
return ListItr<Object>(header->next);
}
void insert(const Object &x, const ListItr<Object> &p)
{
if(p.current != NULL)
p.current->next = new ListNode<Object>(x, p.current->next);
}
ListItr<Object> find(const Object &x) const
{
ListNode<Object> *itr = header->next;
while(itr != NULL && itr->element != x)
itr = itr->next;
return ListItr<Object>(itr);
}
ListItr<Object> findPrevious(const Object &x) const
{
ListNode<Object> *itr = header;
while(itr->next != NULL && itr->next->element != x)
itr = itr->next;
return ListItr<Object>(itr);
}
void remove(const Object &x)
{
ListItr<Object> p = findPrevious(x);
if(p.current->next != NULL)
{
ListNode<Object> *oldNode = p.current->next;
p.current->next = =p.current=>next->next;
delete oldNode;
}
}
const List &operator=(const List &rhs)
{
if(this != &rhs)
{
makeEmpty();
ListItr<Object> ritr = rhs.first();
ListItr<Object> itr = zeroth();
for( ; !ritr.isPastEnd(); ritr.advance(), itr.advance())
insert(ritr.retrieve(), itr);
}
return *this;
}
private:
ListNode<Object> *header;
};
template <class Object>
class ListItr
{
public:
ListItr():current(NULL)
{
}
bool isPastEnd() const
{
return current == NULL;
}
void advance()
{
if(!isPastEnd())
current = current->next;
}
const Object& retrieve() const
{
if(isPastEnd())
throw BadIterator();
return current->element;
}
private:
ListNode<Object> *current; //current pos
ListItr(ListNode<Object> *theNode):current(theNode){}
friend class List<Object>; //granted acess to constructor
};
template <class Object>
class ListNode
{
ListNode(const Object & theElement = Object(), ListNode * n = NULL):element(theElement), next(n){}
Object element;
ListNode *node;
friend class List<Object>;
friend class ListItr<Object>;
};
and i'm getting these errors:
hw1.cpp
E:\CS328\hw1\hw1.cpp(57) : error C2143: syntax error : missing ';' before '<'
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(57) : error C2501: 'ListItr' : missing storage-class or type specifiers
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(57) : error C2059: syntax error : '<'
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(58) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(62) : error C2143: syntax error : missing ';' before '<'
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(62) : error C2501: 'ListItr' : missing storage-class or type specifiers
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(62) : error C2059: syntax error : '<'
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(63) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(67) : error C2143: syntax error : missing ',' before '<'
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(67) : error C2059: syntax error : '<'
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(73) : error C2143: syntax error : missing ';' before '<'
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(73) : error C2501: 'ListItr' : missing storage-class or type specifiers
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(73) : error C2059: syntax error : '<'
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(74) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(83) : error C2143: syntax error : missing ';' before '<'
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(83) : error C2501: 'ListItr' : missing storage-class or type specifiers
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(83) : error C2059: syntax error : '<'
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(84) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(120) : error C2143: syntax error : missing ';' before '<'
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(120) : error C2501: 'ListNode' : missing storage-class or type specifiers
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(120) : error C2059: syntax error : '<'
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(120) : error C2238: unexpected token(s) preceding ';'
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(150) : error C2143: syntax error : missing ';' before '<'
E:\CS328\hw1\hw1.cpp(153) : see reference to class template instantiation 'ListItr<Object>' being compiled
E:\CS328\hw1\hw1.cpp(150) : error C2501: 'ListNode' : missing storage-class or type specifiers
E:\CS328\hw1\hw1.cpp(153) : see reference to class template instantiation 'ListItr<Object>' being compiled
E:\CS328\hw1\hw1.cpp(150) : error C2059: syntax error : '<'
E:\CS328\hw1\hw1.cpp(153) : see reference to class template instantiation 'ListItr<Object>' being compiled
E:\CS328\hw1\hw1.cpp(150) : error C2238: unexpected token(s) preceding ';'
E:\CS328\hw1\hw1.cpp(153) : see reference to class template instantiation 'ListItr<Object>' being compiled
E:\CS328\hw1\hw1.cpp(151) : error C2629: unexpected 'class ListItr<Object> ('
E:\CS328\hw1\hw1.cpp(153) : see reference to class template instantiation 'ListItr<Object>' being compiled
E:\CS328\hw1\hw1.cpp(151) : error C2334: unexpected token(s) preceding ':'; skipping apparent function body
E:\CS328\hw1\hw1.cpp(153) : see reference to class template instantiation 'ListItr<Object>' being compiled
Error executing cl.exe.
hw1.obj - 28 error(s), 0 warning(s)
Any ideas?! This is due in 3 housr, I know it's something simple.
// ----------
// hw1.cpp
// Memo Sanchez
// First Homework Assignment
// ----------
#include <cassert> // assert
#include <cstddef> // size_t
#include <cstring> // strlen
#include <ctime> // CLOCKS_PER_SEC, clock_t, time_t, clock, ctime, time
#include <iostream> // cout, <<, endl
#include <fstream> // ifstream, ofstream
#include <cmath>
#include <algorithm> // copy, max_element, min_element, sort, transform
#include <functional> // bind2nd, less, minus, multiplies, mem_fun_ref
#include <iterator> // back_inserter, istream_iterator, ostream_iterator
#include <memory> // allocator<T>, uninitialized_copy, uninitialized_fill
#include <numeric> // accumulate
#include <stdexcept> // out_of_range
using namespace std;
template <class Object>
class List
{
public:
List()
{
header = new ListNode<Object>;
}
List(const List &rhs)
{
header = new ListNode<Object>;
*this = rhs;
}
~List()
{
makeEmpty();
delete header;
}
bool isEmpty() const
{
return header->next == NULL;
}
void makeEmpty()
{
while(!isEmpty())
remove(first().retrieve());
}
ListItr<Object> zeroth()const
{
return ListItr<Object>(header);
}
ListItr<Object> first()const
{
return ListItr<Object>(header->next);
}
void insert(const Object &x, const ListItr<Object> &p)
{
if(p.current != NULL)
p.current->next = new ListNode<Object>(x, p.current->next);
}
ListItr<Object> find(const Object &x) const
{
ListNode<Object> *itr = header->next;
while(itr != NULL && itr->element != x)
itr = itr->next;
return ListItr<Object>(itr);
}
ListItr<Object> findPrevious(const Object &x) const
{
ListNode<Object> *itr = header;
while(itr->next != NULL && itr->next->element != x)
itr = itr->next;
return ListItr<Object>(itr);
}
void remove(const Object &x)
{
ListItr<Object> p = findPrevious(x);
if(p.current->next != NULL)
{
ListNode<Object> *oldNode = p.current->next;
p.current->next = =p.current=>next->next;
delete oldNode;
}
}
const List &operator=(const List &rhs)
{
if(this != &rhs)
{
makeEmpty();
ListItr<Object> ritr = rhs.first();
ListItr<Object> itr = zeroth();
for( ; !ritr.isPastEnd(); ritr.advance(), itr.advance())
insert(ritr.retrieve(), itr);
}
return *this;
}
private:
ListNode<Object> *header;
};
template <class Object>
class ListItr
{
public:
ListItr():current(NULL)
{
}
bool isPastEnd() const
{
return current == NULL;
}
void advance()
{
if(!isPastEnd())
current = current->next;
}
const Object& retrieve() const
{
if(isPastEnd())
throw BadIterator();
return current->element;
}
private:
ListNode<Object> *current; //current pos
ListItr(ListNode<Object> *theNode):current(theNode){}
friend class List<Object>; //granted acess to constructor
};
template <class Object>
class ListNode
{
ListNode(const Object & theElement = Object(), ListNode * n = NULL):element(theElement), next(n){}
Object element;
ListNode *node;
friend class List<Object>;
friend class ListItr<Object>;
};
and i'm getting these errors:
hw1.cpp
E:\CS328\hw1\hw1.cpp(57) : error C2143: syntax error : missing ';' before '<'
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(57) : error C2501: 'ListItr' : missing storage-class or type specifiers
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(57) : error C2059: syntax error : '<'
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(58) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(62) : error C2143: syntax error : missing ';' before '<'
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(62) : error C2501: 'ListItr' : missing storage-class or type specifiers
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(62) : error C2059: syntax error : '<'
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(63) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(67) : error C2143: syntax error : missing ',' before '<'
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(67) : error C2059: syntax error : '<'
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(73) : error C2143: syntax error : missing ';' before '<'
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(73) : error C2501: 'ListItr' : missing storage-class or type specifiers
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(73) : error C2059: syntax error : '<'
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(74) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(83) : error C2143: syntax error : missing ';' before '<'
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(83) : error C2501: 'ListItr' : missing storage-class or type specifiers
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(83) : error C2059: syntax error : '<'
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(84) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(120) : error C2143: syntax error : missing ';' before '<'
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(120) : error C2501: 'ListNode' : missing storage-class or type specifiers
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(120) : error C2059: syntax error : '<'
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(120) : error C2238: unexpected token(s) preceding ';'
E:\CS328\hw1\hw1.cpp(121) : see reference to class template instantiation 'List<Object>' being compiled
E:\CS328\hw1\hw1.cpp(150) : error C2143: syntax error : missing ';' before '<'
E:\CS328\hw1\hw1.cpp(153) : see reference to class template instantiation 'ListItr<Object>' being compiled
E:\CS328\hw1\hw1.cpp(150) : error C2501: 'ListNode' : missing storage-class or type specifiers
E:\CS328\hw1\hw1.cpp(153) : see reference to class template instantiation 'ListItr<Object>' being compiled
E:\CS328\hw1\hw1.cpp(150) : error C2059: syntax error : '<'
E:\CS328\hw1\hw1.cpp(153) : see reference to class template instantiation 'ListItr<Object>' being compiled
E:\CS328\hw1\hw1.cpp(150) : error C2238: unexpected token(s) preceding ';'
E:\CS328\hw1\hw1.cpp(153) : see reference to class template instantiation 'ListItr<Object>' being compiled
E:\CS328\hw1\hw1.cpp(151) : error C2629: unexpected 'class ListItr<Object> ('
E:\CS328\hw1\hw1.cpp(153) : see reference to class template instantiation 'ListItr<Object>' being compiled
E:\CS328\hw1\hw1.cpp(151) : error C2334: unexpected token(s) preceding ':'; skipping apparent function body
E:\CS328\hw1\hw1.cpp(153) : see reference to class template instantiation 'ListItr<Object>' being compiled
Error executing cl.exe.
hw1.obj - 28 error(s), 0 warning(s)
Any ideas?! This is due in 3 housr, I know it's something simple.