Porting code, need guidance, Thx

Discussion in 'C++' started by Kenton Groombridge, Aug 14, 2004.

  1. Hi,
    I previous posted this in a gcc and g++ groups since that is the
    compiler I am working with, but I didn't get any response.

    Hopefully these are the right groups for this question. I am working
    (actually playing) on porting Alien vs Predator to Linux. I am using
    the code that is available via CVS from icculus.org.

    I recently upgraded my gcc to 3.4.1 and now a portion of the code
    doesn't compile. Nothing in the code has changed.

    I think I figured it out, but want to be sure before I spend a bunch of
    time working around it only to find that I was wrong.

    My guess is the class ConstIterator makes class Iterator a friend, but
    class Iterator is a derived class of the bass class ConstIterator (hope
    I said that right). From what I understand and have researched is that
    in order for the class ConstIterator to use the class Iterator as a
    friend, then class Iterator has to be defined first, and for class
    Iterator to be derived from class ConstIterator, then the class
    ConstIterator has to be defined first. Essentially a catch 22.

    Am I on the right track? If not please point me in the right directly,
    but I would like to fix the code. I would like to know how you would
    approach the fix for this.

    Thanks,
    Ken

    Here is the error:

    g++ -g -Wall -pipe -O2 -DLINUX -Dengine=1 -I. -Iinclude -Iwin95 -Iavp
    -Iavp/win95 -Iavp/support -Iavp/win95/frontend -Iavp/win95/gadgets
    -I/usr/include/SDL -D_REENTRANT -c -o win95/awtexld.o win95/awtexld.cpp
    In file included from win95/awtexld.cpp:10:
    win95/hash_tem.hpp: In member function `void _base_HashTable<TYPE,
    ARG_TYPE, CMP_ARG_TYPE>::Iterator::Remove()':
    win95/hash_tem.hpp:435: error: `nEntriesRemaining' undeclared (first use
    this function)
    win95/hash_tem.hpp:435: error: (Each undeclared identifier is reported
    only once for each function it appears in.)
    win95/hash_tem.hpp:439: error: `nodePP' undeclared (first use this function)
    win95/hash_tem.hpp:446: error: `chainPP' undeclared (first use this
    function)
    win95/hash_tem.hpp:447: error: `nChainsRemaining' undeclared (first use
    this function)
    win95/hash_tem.hpp: In copy constructor `_base_HashTable<TYPE, ARG_TYPE,
    CMP_ARG_TYPE>::_base_HashTable(const _base_HashTable<TYPE, ARG_TYPE,
    CMP_ARG_TYPE>&)':
    win95/hash_tem.hpp:705: error: expected `;' before "it"
    win95/hash_tem.hpp:705: error: `it' undeclared (first use this function)
    make: *** [win95/awtexld.o] Error 1

    Here is the code in question (not very big, but put in this format so
    you can find the lines in question in relation to the errors above):

    http://webpages.charter.net/kgroombr/hash_tem.hpp


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]

    [ comp.std.c++ is moderated. To submit articles, try just posting with ]
    [ your news-reader. If that fails, use mailto: ]
    [ --- Please see the FAQ before posting. --- ]
    [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
     
    Kenton Groombridge, Aug 14, 2004
    #1
    1. Advertising

  2. On Sat, 14 Aug 2004 06:36:00 +0000 (UTC), Kenton Groombridge
    <> wrote:

    >
    > Hi,
    > I previous posted this in a gcc and g++ groups since that is the
    > compiler I am working with, but I didn't get any response.
    >
    > Hopefully these are the right groups for this question. I am working
    > (actually playing) on porting Alien vs Predator to Linux. I am using
    > the code that is available via CVS from icculus.org.
    >
    > I recently upgraded my gcc to 3.4.1 and now a portion of the code
    > doesn't compile. Nothing in the code has changed.
    >
    > I think I figured it out, but want to be sure before I spend a bunch of
    > time working around it only to find that I was wrong.
    >
    > My guess is the class ConstIterator makes class Iterator a friend, but
    > class Iterator is a derived class of the bass class ConstIterator (hope
    > I said that right). From what I understand and have researched is that
    > in order for the class ConstIterator to use the class Iterator as a
    > friend, then class Iterator has to be defined first, and for class
    > Iterator to be derived from class ConstIterator, then the class
    > ConstIterator has to be defined first. Essentially a catch 22.


    Not correct. One class can be declared as a friend of another without
    being declared itself.

    >
    > Am I on the right track? If not please point me in the right directly,
    > but I would like to fix the code. I would like to know how you would
    > approach the fix for this.
    >


    This is a change in the rules for dependent name lookup. It's catching a
    lot of people out but g++ 3.4 is doing the right thing, whereas older
    compilers where not.

    For instance in Iterator prefix all references to names in ConstIterator
    with this->, so

    void Remove()
    {
    if (!this->nEntriesRemaining)
    {
    HT_FAIL("HTT: Tried to Remove() via an iterator which was Done()");
    }
    Node * oldP = *this->nodePP;
    *this->nodePP = oldP->nextP;
    delete oldP;
    if (!*this->nodePP)
    {
    do
    {
    ++ this->chainPP;
    -- this->nChainsRemaining;
    }
    while (this->nChainsRemaining && !*this->chainPP);
    this->nodePP = this->chainPP;
    }
    -- this->nEntriesRemaining;
    -- *tableNEntriesP;
    }

    This is explained in the FAQ

    http://www.parashift.com/c -faq-lite/containers-and-templates.html#faq-34.17

    john
     
    John Harrison, Aug 14, 2004
    #2
    1. Advertising

  3. Kenton Groombridge wrote:
    <snip>
    > I recently upgraded my gcc to 3.4.1 and now a portion of the code
    > doesn't compile. Nothing in the code has changed.
    >
    > I think I figured it out, but want to be sure before I spend a bunch of
    > time working around it only to find that I was wrong.
    >
    > My guess is the class ConstIterator makes class Iterator a friend, but
    > class Iterator is a derived class of the bass class ConstIterator (hope
    > I said that right). From what I understand and have researched is that
    > in order for the class ConstIterator to use the class Iterator as a
    > friend, then class Iterator has to be defined first, and for class
    > Iterator to be derived from class ConstIterator, then the class
    > ConstIterator has to be defined first. Essentially a catch 22.


    That is a problem, but not the problem that's being reported.

    > Am I on the right track? If not please point me in the right directly,
    > but I would like to fix the code. I would like to know how you would
    > approach the fix for this.

    <snip>

    I would be inclined to make ConstIterator's member variables
    protected. This is not generally a good idea, but I wouldn't want to
    go making more drastic changes to the existing code. See
    <http://www.parashift.com/c++-faq-lite/basics-of-inheritance.html#faq-19.8>.

    I think the errors reported by the compiler are the result of the
    change in binding of names in templates that was made in version 3.4
    of GNU C++. (The change was made in the standard long ago.) See
    <http://www.decadentplace.org.uk/womble/cplusplus/template-faq.html#base-lookup>.
    Given this, they can be fixed by adding "this->" before the names of
    the inherited member variables in the Iterator class. The FAQ I
    linked to explains why.

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Ben Hutchings, Aug 14, 2004
    #3
  4. Thanks a bunch,

    This fixed this issue, but now I have another compile problem. I have
    been tinkering with it for several hours but am unsure what it is. The
    error is:

    win95/hash_tem.hpp: In copy constructor `_base_HashTable<TYPE, ARG_TYPE,
    CMP_ARG_TYPE>::_base_HashTable(const _base_HashTable<TYPE, ARG_TYPE,
    CMP_ARG_TYPE>&)':
    win95/hash_tem.hpp:705: error: expected `;' before "it"
    win95/hash_tem.hpp:705: error: `it' undeclared (first use this function)
    win95/hash_tem.hpp:705: error: (Each undeclared identifier is reported
    only once for each function it appears in.)
    win95/hash_tem.hpp:705: error: expected primary-expression before ';' token
    win95/hash_tem.hpp:705: error: expected `)' before ';' token
    win95/hash_tem.hpp:705: error: expected `;' before ')' token
    make: *** [win95/awtexld.o] Error 1

    On line 705 I see where it is calling the copy constructor
    _base_HashTable::ConstIterator it(ht), but all looks good to me. At
    least from what I learned. Any guess what could cause this one? My
    guess is that line 705 is good, but the error lies somewhere in the copy
    constructor, but it also looks good to me.

    I have three C++ books and the most recent one is about seven years old.
    Have a recommendation on one that will bring me up to speed. Most of
    the programs in my books don't compile with gcc 3.4.1 until I do some of
    the code fixes that I found with google searching.

    Thanks again,
    Ken

    John Harrison wrote:

    > Kenton, I never post to moderated groups, in case you missed my reply

    in comp.lang.c++ here it is direct to your inbox.
    >
    > John
    >
     
    Kenton Groombridge, Aug 15, 2004
    #4
  5. Kenton Groombridge

    llewelly Guest

    Kenton Groombridge <> writes:

    > Hi,
    > I previous posted this in a gcc and g++ groups since that is the
    > compiler I am working with, but I didn't get any response.
    >
    > Hopefully these are the right groups for this question. I am working
    > (actually playing) on porting Alien vs Predator to Linux. I am using
    > the code that is available via CVS from icculus.org.
    >
    > I recently upgraded my gcc to 3.4.1 and now a portion of the code
    > doesn't compile. Nothing in the code has changed.


    g++ 3.4 implements two-phase lookup as required by the 1998
    standard. That makes it in some degree source-code incompatible
    with g++ 3.x .

    You can find examples on the gcc website; see the 'C++' section of:
    http://gcc.gnu.org/gcc-3.4/changes.html

    > I think I figured it out, but want to be sure before I spend a bunch of
    > time working around it only to find that I was wrong.
    >
    > My guess is the class ConstIterator makes class Iterator a friend,


    This is true but irrevelant. Friendship affects access control, not
    namelookup. No declaration for 'nEntriesRemaining' is found,
    therefor the issue is namelookup. This is in fact akin to one of
    the examples on the gcc 3.4 changes page I linked to above.

    > but
    > class Iterator is a derived class of the bass class ConstIterator (hope
    > I said that right).


    Correct.

    > From what I understand and have researched is that
    > in order for the class ConstIterator to use the class Iterator as a
    > friend, then class Iterator has to be defined first,


    A definition is *not* necessary. Only a forward declaration is
    necessary - and in some cases the friend declaration itself can
    fulfill this purpose. But the whole friend issue is irrevelant.

    > and for class
    > Iterator to be derived from class ConstIterator, then the class
    > ConstIterator has to be defined first.


    True.

    > Essentially a catch 22.


    No, becuase declaring something friend does not require a definition
    - but, as I said before, the whole friend issue is irrevelant.

    [snip]
    > I would like to know how you would
    > approach the fix for this.

    [snip]

    Replace 'nEntriesRemaining' whith 'this->nEntriesRemaining'.

    The Vandervoode and Josuttis _C++ Templates_ book has a section on
    dependent names which I think explains why this works.


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]

    [ comp.std.c++ is moderated. To submit articles, try just posting with ]
    [ your news-reader. If that fails, use mailto: ]
    [ --- Please see the FAQ before posting. --- ]
    [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
     
    llewelly, Aug 16, 2004
    #5
  6. I finally figured this one out. I got to get used to the stricter rules
    of the C++ standard. I also need some new books which stick to these
    standards since most of the programs in my books no longer compile with
    gcc 3.1. Been programming the wrong way for a long time. Anybody that
    has book recommendations that stick with the strict C++ standard, please
    respond. All my books are at least 7 to 12 years old.

    Here is the fix for anybody that has issue with anything like this:

    The error is on line 705 which is:

    for (_base_HashTable::ConstIterator it(ht); !it.Done(); it.Next() )


    The fix is:

    for (typename _base_HashTable::ConstIterator it(ht); !it.Done();
    it.Next() )

    as found on:

    http://gcc.gnu.org/gcc-3.4/changes.html

    which states:

    You must now use the typename and template keywords to disambiguate
    dependent names, as required by the C++ standard.


    Kenton Groombridge wrote:
    > Thanks a bunch,
    >
    > This fixed this issue, but now I have another compile problem. I have
    > been tinkering with it for several hours but am unsure what it is. The
    > error is:
    >
    > win95/hash_tem.hpp: In copy constructor `_base_HashTable<TYPE, ARG_TYPE,
    > CMP_ARG_TYPE>::_base_HashTable(const _base_HashTable<TYPE, ARG_TYPE,
    > CMP_ARG_TYPE>&)':
    > win95/hash_tem.hpp:705: error: expected `;' before "it"
    > win95/hash_tem.hpp:705: error: `it' undeclared (first use this function)
    > win95/hash_tem.hpp:705: error: (Each undeclared identifier is reported
    > only once for each function it appears in.)
    > win95/hash_tem.hpp:705: error: expected primary-expression before ';' token
    > win95/hash_tem.hpp:705: error: expected `)' before ';' token
    > win95/hash_tem.hpp:705: error: expected `;' before ')' token
    > make: *** [win95/awtexld.o] Error 1
    >
     
    Kenton Groombridge, Aug 18, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hercules
    Replies:
    6
    Views:
    3,635
    Kevin Bracey
    Dec 2, 2003
  2. Joe Wright

    Re: [OT] "thx"

    Joe Wright, Apr 1, 2004, in forum: C Programming
    Replies:
    0
    Views:
    417
    Joe Wright
    Apr 1, 2004
  3. Old Wolf
    Replies:
    3
    Views:
    328
    Ben Measures
    Apr 3, 2004
  4. Kentor
    Replies:
    47
    Views:
    1,234
    derbz
    Feb 16, 2007
  5. fAnSKyer

    Help in java script code, THX :P

    fAnSKyer, Jul 25, 2007, in forum: Javascript
    Replies:
    3
    Views:
    125
    David Mark
    Jul 25, 2007
Loading...

Share This Page