Results 1 to 20 of 20

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

  1. #1
    Admiral Memo's Avatar
    Join Date
    Apr 2001
    Location
    East Village
    Posts
    5,659

    c++ help, assignment due in 3 hours, haven't progged in a year

    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.

  2. #2
    the lemonizer sho.gun's Avatar
    Join Date
    Apr 2001
    Location
    Calabasas, CA
    Posts
    5,374
    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/under...ists/lec05.pdf

  3. #3
    Captain hang10wannabe's Avatar
    Join Date
    Jul 2002
    Location
    OxnardiA
    Posts
    1,884
    ummm riiiiiiight... i got nothing...

    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?

  4. #4
    hot in velour pants Burzhui's Avatar
    Join Date
    Jun 2000
    Location
    New York City
    Posts
    9,200
    looks like simple syntax errors, i don't have a c++ compiler at home, but go over the code and double check
    ____________________
    IF A FAT GIRL FALLS IN THE WOODS
    DO THE TREES LAUGH?

  5. #5
    Lieutenant ShortStack's Avatar
    Join Date
    Jul 2002
    Location
    College Planet with the rest of the idiots
    Posts
    306
    Originally posted by hang10wannabe
    ummm riiiiiiight... i got nothing...

    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?
    You changed your avatar! How could you! You're confusing me!!
    Short Stack
    Website Goddess
    www.shortstackdesigns.com


    "I'm not crazy i'm just a little unwell
    I know right now you can't tell
    But stay awhile and maybe then you'll see
    A different side of me
    I'm not crazy i'm just a little impaired
    I know right now you don't care
    But soon enough you're gonna think of me
    And how i used to be"
    - Unwell, Matchbox Twenty

  6. #6
    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.
    w00t, i'm a writer for passwird.com

  7. #7
    Old Skooler Numba 1 eSDee's Avatar
    Join Date
    Nov 2000
    Location
    Diego
    Posts
    10,065
    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? Just go through the code line by line. It looks like a simple syntax error bro.
    ~~~~~~~~~~~~
    3 days ~ Willie Nelson

    3 days I dread to see arrive
    3 days I hate to be alive
    3 days filled with tears and sorrow
    yesterday today and tomorrow

  8. #8
    Fleet Admiral hapoo's Avatar
    Join Date
    Jan 2000
    Location
    742 Evergreen Terrace, Springfield USA
    Posts
    9,276
    use MS's developer page to find what errors mean:
    http://msdn.microsoft.com/library/de...HTML/c2143.asp

    just search for the error code.



    but yeah, it looks like you just miscopied a couple parts of it
    Last edited by hapoo; 09-18-2002 at 08:35 PM.

  9. #9
    Admiral Memo's Avatar
    Join Date
    Apr 2001
    Location
    East Village
    Posts
    5,659
    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.

  10. #10
    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.
    w00t, i'm a writer for passwird.com

  11. #11
    Admiral Memo's Avatar
    Join Date
    Apr 2001
    Location
    East Village
    Posts
    5,659
    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.

  12. #12
    Fleet Admiral hapoo's Avatar
    Join Date
    Jan 2000
    Location
    742 Evergreen Terrace, Springfield USA
    Posts
    9,276
    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)

  13. #13
    Captain hang10wannabe's Avatar
    Join Date
    Jul 2002
    Location
    OxnardiA
    Posts
    1,884
    Originally posted by ShortStack


    You changed your avatar! How could you! You're confusing me!!
    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


  14. #14
    Lieutenant Commander sleepminded's Avatar
    Join Date
    Jul 2001
    Location
    in a straight jacket, tied up in a padded cell
    Posts
    968
    i done almost no c++ cept for play around with it and mess up my progs by means of random debuggin
    m00...ph34r t3h n00b

    [-=1manclan=-]n00b

  15. #15
    shibuya girl
    Join Date
    Apr 2000
    Location
    Oregon
    Posts
    6,855
    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.

  16. #16
    Admiral Memo's Avatar
    Join Date
    Apr 2001
    Location
    East Village
    Posts
    5,659
    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 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;
    }

  17. #17
    Rear Admiral Upper Half ribitch's Avatar
    Join Date
    Sep 2001
    Location
    Michigan
    Posts
    3,672
    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.

  18. #18
    Fleet Admiral hapoo's Avatar
    Join Date
    Jan 2000
    Location
    742 Evergreen Terrace, Springfield USA
    Posts
    9,276
    i used VC++ 6.0 and got the same exact error, thats probably what he used also

  19. #19
    Admiral Memo's Avatar
    Join Date
    Apr 2001
    Location
    East Village
    Posts
    5,659
    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.

  20. #20
    Lieutenant ShortStack's Avatar
    Join Date
    Jul 2002
    Location
    College Planet with the rest of the idiots
    Posts
    306
    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

    Nah, she's wearing a chastity belt.
    Short Stack
    Website Goddess
    www.shortstackdesigns.com


    "I'm not crazy i'm just a little unwell
    I know right now you can't tell
    But stay awhile and maybe then you'll see
    A different side of me
    I'm not crazy i'm just a little impaired
    I know right now you don't care
    But soon enough you're gonna think of me
    And how i used to be"
    - Unwell, Matchbox Twenty

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •