PDA

View Full Version : c++ help, assignment due in 3 hours, haven't progged in a year



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.

sho.gun
09-18-2002, 07:16 PM
geez i'd like to help but I haven't even learned C++ that far yet, check this real quick though it seems like it has something to do about the stuff you're doing:

http://www.cs.umbc.edu/courses/undergraduate/341/fall00/Lectures/Lists/lec05.pdf

hang10wannabe
09-18-2002, 07:20 PM
ummm riiiiiiight... i got nothing... :shrug:

im in the same class as sho.gun and if he has no clue... he knows more than i so... im useless in this situation... got any questions on maki gotoh? :bandit:

Burzhui
09-18-2002, 07:52 PM
looks like simple syntax errors, i don't have a c++ compiler at home, but go over the code and double check

ShortStack
09-18-2002, 07:55 PM
Originally posted by hang10wannabe
ummm riiiiiiight... i got nothing... :shrug:

im in the same class as sho.gun and if he has no clue... he knows more than i so... im useless in this situation... got any questions on maki gotoh? :bandit:

You changed your avatar! How could you! You're confusing me!! :bawl:

joepunk
09-18-2002, 08:20 PM
hey, i just looked at the code for literally a second (i have so much work to be done that's all the time i can afford) but you didn't add the .h extension to the libraries for the header files.

i.e. #include <iostream> should be #include <iostream.h>

honestly, i dunno if this makes a difference since you may be using a more advanced compiler than myself where it doesn't matter, but just try it.

i'll try to look at it more in the next few mins.

eSDee
09-18-2002, 08:26 PM
Wait a second, you have the book right there with the correct way to write it, and you want us to guess what the problem is? :dodgy: Just go through the code line by line. It looks like a simple syntax error bro.

hapoo
09-18-2002, 08:32 PM
use MS's developer page to find what errors mean:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/HTML/c2143.asp

just search for the error code.



but yeah, it looks like you just miscopied a couple parts of it

Memo
09-18-2002, 08:42 PM
the code is exactly what is in the book, it is not a syntax error. a syntax error isn't replicated 28 times :( I've already checked it with the book 3 times and re-written it all over as well. Checked it with 2 other books, no go. I believe it has something to do with the paramterizing of the class with the template, but it's been a year since i made any template functions. It's due in 1 hour now and it won't compile, I guess i'll take the 0. :(

joepunk
09-18-2002, 08:48 PM
Dude, did you try to add the .h to your header statements? Because I believe that may be it. Just try it, if it doesn't work, I'll look for further stuff.

Memo
09-18-2002, 08:49 PM
Originally posted by joepunk
Dude, did you try to add the .h to your header statements? Because I believe that may be it. Just try it, if it doesn't work, I'll look for further stuff.

ya, i did try it, wasn't it. you don't need to add .h. just add the std namespace.

hapoo
09-18-2002, 08:54 PM
Originally posted by joepunk
Dude, did you try to add the .h to your header statements? Because I believe that may be it. Just try it, if it doesn't work, I'll look for further stuff.

when theres a header error it will tell you!!!
its NOT a header problem!




exp.cpp(16) : fatal error C1083: Cannot open include file: 'functional.h': No such file or directory
Error executing cl.exe.

exp.exe - 1 error(s), 0 warning(s)

hang10wannabe
09-18-2002, 09:41 PM
Originally posted by ShortStack


You changed your avatar! How could you! You're confusing me!! :bawl:

hehe... yea ryu's cool to a point... but this guys confused face is great... he maybe looking up chun-li's dress in ur avatar :eek:

:angel:

sleepminded
09-18-2002, 10:09 PM
i done almost no c++ cept for play around with it and mess up my progs by means of random debuggin :D

revil
09-19-2002, 02:23 AM
haven't played with c++ too much but as far as i can tell, there is a syntax error here:

ListItr<Object> zeroth()const

the first error points out that it wants a ; before the <, so the syntax error probably has to do with:

ListItr<Object> zeroth()const

not exactly sure what is wrong though. sorry.

Memo
09-19-2002, 07:46 AM
Guys, I figured it out last nite, although not in time to turn in the assignment (it's ok was worth 5 points ,all the other assignments are worth 20). The problem was in my implementation, i didn't have to paramterize the class everytime i wrote it. Also, I went ahead and moved the other classes inside the larger List class to give them access. Compiles fine in visual c++ and most compilers but won't compile in unix, I get an error saying a class has been converted to a Struct. the GCC unix compiler is really picky :rolleyes: so I need to figure that out since the next program is based on this. Anyways, thanks for all the help guys and if you want to see the code a singly-linked tempalte based list there it is:

// ----------
// 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 T>
class List
{

public:

class ListNode
{
public:
ListNode(const T & theElement = T(), ListNode * n = NULL) : element( theElement ), next( n ) {}

T element;
ListNode *next;

friend class List;//*****
friend class ListItr;//***
};



//***BEGIN ITERATOR CLASS****
class ListItr
{
public:

ListItr() : current(NULL){}

bool isPastEnd() const
{
return current == NULL;
}

void advance()
{
if(!isPastEnd())
{
current = current->next;
}
}

const T& retrieve() const
{
if(isPastEnd())
cout << "PAST END!" << endl;
return current->element;
}

///private:

ListNode *current;
ListItr(ListNode *theNode) : current(theNode){}
friend class List; // ***MAYBE <T>


};
//***END ITERATOR CLASS****



List()
{
header = new ListNode;
}

List(const List &rhs)
{
header = new ListNode;
*this = rhs;
}

~List()
{
makeEmpty();
delete header;
}

void printList(const List &theList)
{
if(theList.isEmpty())
cout << "Empty List" << endl;
else
{
ListItr itr = theList.first();
for( ; !itr.isPastEnd(); itr.advance())
cout << itr.retrieve() << " ";
}

cout << endl;
}


bool isEmpty() const
{
return header->next ==NULL;
}

void makeEmpty()
{
while(!isEmpty())
remove(first().retrieve());
}

ListItr zeroth() const
{
return ListItr(header);
}

ListItr first() const
{
return ListItr(header->next);
}

void insert(const T& x, const ListItr &p)
{
if(p.current != NULL)
p.current->next = new ListNode(x, p.current->next);
}

ListItr find(const T& x) const
{
ListNode *itr = header->next;

while(itr != NULL && itr->element != x)
itr = itr->next;

return ListItr(itr);
}

ListItr findPrevious(const T& x) const
{
ListNode *itr = header;

while(itr->next != NULL && itr->next->element != x)
itr=itr->next;

return ListItr(itr);
}

void remove(const T& x)
{
ListItr p = findPrevious(x);

if(p.current->next != NULL)
{
ListNode *oldNode = p.current->next;
p.current->next = p.current->next->next;
delete oldNode;
}
}

const List& operator=(const List &rhs)
{
if(this != &rhs)
{
makeEmpty();

ListItr ritr = rhs.first();
ListItr itr = zeroth();
for( ; !ritr.isPastEnd(); ritr.advance(), itr.advance())
insert(ritr.retrieve(), itr);
}
return *this;
}


private:

ListNode *header;



};



int main()
{
List<int> x;

for(int i = 0; i <= 100; i++)
{
if(!(i%2))
x.insert(i, x.zeroth());
}

x.printList(x);

return 0;
}

ribitch
09-19-2002, 01:19 PM
wow, looks like you are programming on an MS box. Are you ising visual studio, borland, or what?

I havent programmed anything on windows in forever. Last thing was for a VB class. Our school pushes unix.

hapoo
09-19-2002, 01:40 PM
i used VC++ 6.0 and got the same exact error, thats probably what he used also

Memo
09-19-2002, 02:37 PM
I used VC++ last nite cause I Was in a hurry and didn't have tiem to install DJGPP. I did check loginto a sun shell and try to compile it there as well.

ShortStack
09-23-2002, 01:59 AM
Originally posted by hang10wannabe


hehe... yea ryu's cool to a point... but this guys confused face is great... he maybe looking up chun-li's dress in ur avatar :eek:

:angel:

:stupid: Nah, she's wearing a chastity belt.