Incorrect template code, or compiler/library bug?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael T Decerbo

    Incorrect template code, or compiler/library bug?

    The code below compiles without warnings or errors under g++ 3.2.1 on
    Solaris, gcc 3.2.2 on Linux, and MSVC 6.0 on Windows.

    Under g++ (GCC) 3.3.2, it produces the following errors:

    % g++ -Wall testme_std.cpp
    testme_std.cpp: 24: error: no `I LblID<I, MaxInd>::add(co nst
    std::basic_stri ng<char, std::char_trait s<char>, std::allocator< char> >&)'
    member function declared in class `LblID<I, MaxInd>'
    testme_std.cpp: 24: error: template definition of non-template `I LblID<I,
    MaxInd>::add(co nst std::basic_stri ng<char, std::char_trait s<char>,
    std::allocator< char> >&)'
    testme_std.cpp: In member function `I LblID<I, MaxInd>::add(co nst
    std::basic_stri ng<char, std::char_trait s<char>, std::allocator< char> >&)':
    testme_std.cpp: 15: error: `typedef typename std::map<std::s tring, I,
    std::less<std:: string>, std::allocator< std::pair<const std::string, I> >[color=blue]
    >::iterator LblID<I, MaxInd>::lbl_it er' is inaccessible[/color]
    testme_std.cpp: 25: error: within this context

    Is the error in the code, or in the compiler?

    In general, is there some way I can answer this question in the future--
    an online syntax checker, or something?

    Thanks, everyone.


    Mike Decerbo
    mike@mit.edu

    (code follows)

    #include <map>
    #include <vector>
    #include <string>

    using namespace std;

    template <class I, size_t MaxInd>
    class LblID
    {
    public:

    map< string, I > lbl2id;
    vector<string> id2lbl;

    typedef typename map< string, I >::iterator lbl_iter;

    I add(const string& lbl);

    };


    template <class I, unsigned int MaxInd>
    I LblID<I,MaxInd> ::add(const string& lbl)
    {
    lbl_iter lbli = lbl2id.find(lbl );
    if (lbli.compare(l bl2id.end()) != 0)
    return (*lbli).second;

    if (size() >= MaxInd)
    return MaxInd;

    I id = size();
    lbl2id[lbl] = id;
    id2lbl.push_bac k(lbl);
    return id;
    }

    int main (int argc, char *argv[])
    {

    }




  • Julián Albo

    #2
    Re: Incorrect template code, or compiler/library bug?

    Michael T Decerbo escribió:
    [color=blue]
    > The code below compiles without warnings or errors under g++ 3.2.1 on
    > Solaris, gcc 3.2.2 on Linux, and MSVC 6.0 on Windows.
    >
    > Under g++ (GCC) 3.3.2, it produces the following errors:[/color]

    Compiles fine for me with:

    # gcc --version
    gcc (GCC) 3.3 20030226 (prerelease) (SuSE Linux)

    Perhaps you are using the standard library corresponding to other
    version?

    Regards.

    Comment

    • grejdanospam@pacbell.net

      #3
      Re: Incorrect template code, or compiler/library bug?

      On 21 Oct 2003 19:35:51 GMT, Michael T Decerbo <mike@mit.edu > wrote:
      [color=blue]
      > The code below compiles without warnings or errors under g++ 3.2.1 on
      > Solaris, gcc 3.2.2 on Linux, and MSVC 6.0 on Windows.
      >
      > Under g++ (GCC) 3.3.2, it produces the following errors:
      >
      > % g++ -Wall testme_std.cpp
      > testme_std.cpp: 24: error: no `I LblID<I, MaxInd>::add(co nst
      > std::basic_stri ng<char, std::char_trait s<char>, std::allocator< char> >&)'
      > member function declared in class `LblID<I, MaxInd>'
      > testme_std.cpp: 24: error: template definition of non-template `I LblID<I,
      > MaxInd>::add(co nst std::basic_stri ng<char, std::char_trait s<char>,[/color]
      // cut[color=blue]
      >
      > using namespace std;
      >
      > template <class I, size_t MaxInd>
      > class LblID[/color]

      // cut[color=blue]
      >
      > template <class I, unsigned int MaxInd>
      > I LblID<I,MaxInd> ::add(const string& lbl)
      > {
      > lbl_iter lbli = lbl2id.find(lbl );
      > if (lbli.compare(l bl2id.end()) != 0)
      > return (*lbli).second;
      > if (size() >= MaxInd)
      > return MaxInd;
      > I id = size();
      > lbl2id[lbl] = id;
      > id2lbl.push_bac k(lbl);
      > return id;
      > }
      >
      > int main (int argc, char *argv[])
      > {
      >
      > }
      >
      >
      >
      >
      >[/color]
      is the size_t the same type as unsigned int ?


      --
      grzegorz

      Comment

      • Michael T Decerbo

        #4
        Re: Incorrect template code, or compiler/library bug?

        In article <3F958C51.D4D4F B7C@terra.es>,
        =?iso-8859-1?Q?Juli=E1n?= Albo <JULIANALBO@ter ra.es> wrote:[color=blue][color=green]
        >> Under g++ (GCC) 3.3.2, it produces the following errors:[/color]
        >
        >Compiles fine for me with:
        >
        ># gcc --version
        >gcc (GCC) 3.3 20030226 (prerelease) (SuSE Linux)
        >
        >Perhaps you are using the standard library corresponding to other
        >version?[/color]

        You are correct. The system I was using is an Apple Mac OS X machine,
        which has some version of the standard library installed under
        /usr/include/gcc.

        On a Linux system with a fresh install of 3.3.2 there is no error.

        Thanks for the help!


        Mike

        --
        Michael Decerbo --- mike@mit.edu

        Comment

        Working...