Some problems with std::vector and nested templates

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jubelbrus

    Some problems with std::vector and nested templates

    Hi

    I'm trying to do the following.

    #include <vector>
    #include <boost/thread/mutex.hpp>
    #include <boost/shared_ptr.hpp>
    #include <boost/tuple/tuple.hpp>

    class {
    boost::mutex m_lock;
    std::vector<boo st::tuple<uint3 2_t, boost::shared_p tr<T >
    m_vector;

    template <class Tboost::shared_ ptr<Tlist::<T>g etitem(uint32_t id)
    {
    boost::mutex::s coped_lock lock(m_lock);

    std::vector< boost::tuple<ui nt32_t, boost::shared_p tr<T
    >::iterator pos =
    find_if(m_vecto r.begin(), m_vector.end(), compare_list<T> (id),
    m_vector.begin( ));

    if (pos != m_vector.end())
    return *pos;

    return boost::shared_p tr(NULL);
    };

    but I get the following error
    error: expected ';' before 'pos'

    I don't understand why I'm getting this error because the definition
    of std::vector works, it's just the iterator statement that doesn't.

  • Robert Bauck Hamar

    #2
    Re: Some problems with std::vector and nested templates

    jubelbrus wrote:
    Hi
    >
    I'm trying to do the following.
    >
    #include <vector>
    #include <boost/thread/mutex.hpp>
    #include <boost/shared_ptr.hpp>
    #include <boost/tuple/tuple.hpp>
    >
    class {
    boost::mutex m_lock;
    std::vector<boo st::tuple<uint3 2_t, boost::shared_p tr<T >
    m_vector;
    I'm assuming uint32_t might be typedefed somewhere.
    What's T? This is not a template?
    template <class Tboost::shared_ ptr<Tlist::<T>g etitem(uint32_t id)
    huh?
    template <class T>
    boost::shared_p tr<T>
    list<T>::getite m(uint32_t id)
    ?
    What's list?
    {
    boost::mutex::s coped_lock lock(m_lock);
    >
    std::vector< boost::tuple<ui nt32_t, boost::shared_p tr<T
    >>::iterator pos =
    find_if(m_vecto r.begin(), m_vector.end(), compare_list<T> (id),
    m_vector.begin( ));
    typename std::vector<boo st::tuple<uint3 2_t, boost::shared_p tr<T
    >::iterator pos = find_if(...)
    You need typename here, because vector< ... is dependent on T.
    if (pos != m_vector.end())
    return *pos;
    *pos is a tuple, not a shared_ptr
    return boost::shared_p tr(NULL);
    };
    >
    but I get the following error
    error: expected ';' before 'pos'
    >
    I don't understand why I'm getting this error because the definition
    of std::vector works, it's just the iterator statement that doesn't.
    --
    rbh

    Comment

    • jubelbrus

      #3
      Re: Some problems with std::vector and nested templates

      Sorry about the misspellings...

      This is what I'm trying to do:
      Write a template class that stores a list of boost::shared_p tr<Twith
      a index and is thread safe, where T would be that template <class T>
      stuff..

      I got the getitem function compiling by using typename, tanks.. but
      I've discovered some compile problems with my insert code.

      #include <stdint.h>
      #include <vector>
      #include <boost/thread/mutex.hpp>
      #include <boost/shared_ptr.hpp>
      #include <boost/tuple/tuple.hpp>

      template <class T>
      class list{
      boost::mutex m_lock;
      std::vector<boo st::tuple<uint3 2_t, boost::shared_p tr<T >
      m_vector;

      uint32_t m_nextid;

      uint32_t insert(boost::s hared_ptr<T>);
      }

      template <class T>
      uint32_t list<T>::insert (boost::shared_ ptr<T>)
      {
      typename boost::tuples:: tuple<uint32_t, boost::shared_p tr<T
      add_item(m_next id, item);
      typename std::vector<boo st::tuple<uint3 2_t, boost::shared_p tr<T
      m_vector;
      boost::mutex::s coped_lock lock(m_lock);

      m_vector.insert ( add_item );
      return m_nextid++;

      };

      I get the following error:
      list.h: In member function 'uint32_t
      stdtools::list< T>::insert(boos t::shared_ptr<T >) [with T = int]':
      tester_a.cpp:10 : instantiated from here
      list.h:90: error: no matching function for call to
      'std::vector<bo ost::tuples::tu ple<uint32_t, boost::shared_p tr<int>,
      boost::tuples:: null_type, boost::tuples:: null_type,
      boost::tuples:: null_type, boost::tuples:: null_type,
      boost::tuples:: null_type, boost::tuples:: null_type,
      boost::tuples:: null_type, boost::tuples:: null_type>,
      std::allocator< boost::tuples:: tuple<uint32_t, boost::shared_p tr<int>,
      boost::tuples:: null_type, boost::tuples:: null_type,
      boost::tuples:: null_type, boost::tuples:: null_type,
      boost::tuples:: null_type, boost::tuples:: null_type,
      boost::tuples:: null_type, boost::tuples:: null_type
      >::insert(boost ::tuples::tuple <uint32_t, boost::shared_p tr<int>,
      boost::tuples:: null_type, boost::tuples:: null_type,
      boost::tuples:: null_type, boost::tuples:: null_type,
      boost::tuples:: null_type, boost::tuples:: null_type,
      boost::tuples:: null_type, boost::tuples:: null_type>&)'
      /usr/include/c++/4.0.0/bits/vector.tcc:93: note: candidates are:
      typename std::vector<_Tp , _Alloc>::iterat or std::vector<_Tp ,
      _Alloc>::insert (__gnu_cxx::__n ormal_iterator< typename _Alloc::pointer ,
      std::vector<_Tp , _Alloc, const _Tp&) [with _Tp =
      boost::tuples:: tuple<uint32_t, boost::shared_p tr<int>,
      boost::tuples:: null_type, boost::tuples:: null_type,
      boost::tuples:: null_type, boost::tuples:: null_type,
      boost::tuples:: null_type, boost::tuples:: null_type,
      boost::tuples:: null_type, boost::tuples:: null_type>, _Alloc =
      std::allocator< boost::tuples:: tuple<uint32_t, boost::shared_p tr<int>,
      boost::tuples:: null_type, boost::tuples:: null_type,
      boost::tuples:: null_type, boost::tuples:: null_type,
      boost::tuples:: null_type, boost::tuples:: null_type,
      boost::tuples:: null_type, boost::tuples:: null_type]
      /usr/include/c++/4.0.0/bits/stl_vector.h:65 7: note:
      void std::vector<_Tp ,
      _Alloc>::insert (__gnu_cxx::__n ormal_iterator< typename _Alloc::pointer ,
      std::vector<_Tp , _Alloc, size_t, const _Tp&) [with _Tp =
      boost::tuples:: tuple<uint32_t, boost::shared_p tr<int>,
      boost::tuples:: null_type, boost::tuples:: null_type,
      boost::tuples:: null_type, boost::tuples:: null_type,
      boost::tuples:: null_type, boost::tuples:: null_type,
      boost::tuples:: null_type, boost::tuples:: null_type>, _Alloc =
      std::allocator< boost::tuples:: tuple<uint32_t, boost::shared_p tr<int>,
      boost::tuples:: null_type, boost::tuples:: null_type,
      boost::tuples:: null_type, boost::tuples:: null_type,
      boost::tuples:: null_type, boost::tuples:: null_type,
      boost::tuples:: null_type, boost::tuples:: null_type]

      Anybody??

      Comment

      • John Harrison

        #4
        Re: Some problems with std::vector and nested templates

        jubelbrus wrote:
        Sorry about the misspellings...
        >
        This is what I'm trying to do:
        Write a template class that stores a list of boost::shared_p tr<Twith
        a index and is thread safe, where T would be that template <class T>
        stuff..
        >
        I got the getitem function compiling by using typename, tanks.. but
        I've discovered some compile problems with my insert code.
        >
        #include <stdint.h>
        #include <vector>
        #include <boost/thread/mutex.hpp>
        #include <boost/shared_ptr.hpp>
        #include <boost/tuple/tuple.hpp>
        >
        template <class T>
        class list{
        boost::mutex m_lock;
        std::vector<boo st::tuple<uint3 2_t, boost::shared_p tr<T >
        m_vector;
        >
        uint32_t m_nextid;
        >
        uint32_t insert(boost::s hared_ptr<T>);
        }
        >
        template <class T>
        uint32_t list<T>::insert (boost::shared_ ptr<T>)
        {
        typename boost::tuples:: tuple<uint32_t, boost::shared_p tr<T
        add_item(m_next id, item);
        typename std::vector<boo st::tuple<uint3 2_t, boost::shared_p tr<T
        >m_vector;
        >
        boost::mutex::s coped_lock lock(m_lock);
        >
        m_vector.insert ( add_item );
        Perhaps you mean

        m_vector.push_b ack( add_item );

        There is no method of vector called insert that takes a single argument.

        john

        Comment

        • jubelbrus

          #5
          Re: Some problems with std::vector and nested templates

          On 17 Jun, 17:49, John Harrison <john_androni.. .@hotmail.comwr ote:
          jubelbrus wrote:
          Sorry about the misspellings...
          >
          This is what I'm trying to do:
          Write a template class that stores a list of boost::shared_p tr<Twith
          a index and is thread safe, where T would be that template <class T>
          stuff..
          >
          I got the getitem function compiling by using typename, tanks.. but
          I've discovered some compile problems with my insert code.
          >
          #include <stdint.h>
          #include <vector>
          #include <boost/thread/mutex.hpp>
          #include <boost/shared_ptr.hpp>
          #include <boost/tuple/tuple.hpp>
          >
          template <class T>
          class list{
          boost::mutex m_lock;
          std::vector<boo st::tuple<uint3 2_t, boost::shared_p tr<T >
          m_vector;
          >
          uint32_t m_nextid;
          >
          uint32_t insert(boost::s hared_ptr<T>);
          }
          >
          template <class T>
          uint32_t list<T>::insert (boost::shared_ ptr<T>)
          {
          typename boost::tuples:: tuple<uint32_t, boost::shared_p tr<T
          add_item(m_next id, item);
          typename std::vector<boo st::tuple<uint3 2_t, boost::shared_p tr<T
          m_vector;
          >
          boost::mutex::s coped_lock lock(m_lock);
          >
          m_vector.insert ( add_item );
          >
          Perhaps you mean
          >
          m_vector.push_b ack( add_item );
          >
          There is no method of vector called insert that takes a single argument.
          >
          john
          oh... :)

          thanks

          Comment

          • Robert Bauck Hamar

            #6
            Re: Some problems with std::vector and nested templates

            jubelbrus wrote:
            Sorry about the misspellings...
            >
            This is what I'm trying to do:
            Write a template class that stores a list of boost::shared_p tr<Twith
            a index and is thread safe, where T would be that template <class T>
            stuff..
            >
            I got the getitem function compiling by using typename, tanks.. but
            I've discovered some compile problems with my insert code.
            >
            #include <stdint.h>
            #include <vector>
            #include <boost/thread/mutex.hpp>
            #include <boost/shared_ptr.hpp>
            #include <boost/tuple/tuple.hpp>
            >
            template <class T>
            class list{
            boost::mutex m_lock;
            std::vector<boo st::tuple<uint3 2_t, boost::shared_p tr<T >
            m_vector;
            >
            uint32_t m_nextid;
            >
            uint32_t insert(boost::s hared_ptr<T>);
            }
            >
            template <class T>
            uint32_t list<T>::insert (boost::shared_ ptr<T>)
            {
            typename boost::tuples:: tuple<uint32_t, boost::shared_p tr<T
            add_item(m_next id, item);
            There should be no typename here.
            typename std::vector<boo st::tuple<uint3 2_t, boost::shared_p tr<T
            >m_vector;
            nor here.

            typename are needed in:
            T::something
            or
            someclass<T>::s omething
            To put it very short.

            This is because the compiler cannot possibly know what something is. The
            first is obvious. The second:

            template <class T>
            class someclass {
            public:
            typedef T something;
            };

            int p;

            template <class T>
            void foo() {
            someclass<T>::s omething * p; /*Missing typename and definition of local p
            or multiplication? Standard says multiplication */
            }

            template <>
            class someclass<int{
            public:
            int something;
            };

            int someclass<int>: :something = 3;

            int main() {
            p = 42;
            foo<int>();
            }
            boost::mutex::s coped_lock lock(m_lock);
            >
            m_vector.insert ( add_item );
            return m_nextid++;
            >
            };
            >
            I get the following error:
            list.h: In member function 'uint32_t
            stdtools::list< T>::insert(boos t::shared_ptr<T >) [with T = int]':
            tester_a.cpp:10 : instantiated from here
            list.h:90: error: no matching function for call to
            // beginning
            'std::vector<bo ost::tuples::tu ple<uint32_t, boost::shared_p tr<int>,
            boost::tuples:: null_type, boost::tuples:: null_type,
            boost::tuples:: null_type, boost::tuples:: null_type,
            boost::tuples:: null_type, boost::tuples:: null_type,
            boost::tuples:: null_type, boost::tuples:: null_type>,
            std::allocator< boost::tuples:: tuple<uint32_t, boost::shared_p tr<int>,
            boost::tuples:: null_type, boost::tuples:: null_type,
            boost::tuples:: null_type, boost::tuples:: null_type,
            boost::tuples:: null_type, boost::tuples:: null_type,
            boost::tuples:: null_type, boost::tuples:: null_type
            >>::insert(boos t::tuples::tupl e<uint32_t, boost::shared_p tr<int>,
            boost::tuples:: null_type, boost::tuples:: null_type,
            boost::tuples:: null_type, boost::tuples:: null_type,
            boost::tuples:: null_type, boost::tuples:: null_type,
            boost::tuples:: null_type, boost::tuples:: null_type>&)'
            //end

            Shorter:
            std::vector<... >::insert(tuple <...>)
            /usr/include/c++/4.0.0/bits/vector.tcc:93: note: candidates are:
            //beginning
            typename std::vector<_Tp , _Alloc>::iterat or std::vector<_Tp ,
            _Alloc>::insert (__gnu_cxx::__n ormal_iterator< typename _Alloc::pointer ,
            std::vector<_Tp , _Alloc, const _Tp&) [with _Tp =
            boost::tuples:: tuple<uint32_t, boost::shared_p tr<int>,
            boost::tuples:: null_type, boost::tuples:: null_type,
            boost::tuples:: null_type, boost::tuples:: null_type,
            boost::tuples:: null_type, boost::tuples:: null_type,
            boost::tuples:: null_type, boost::tuples:: null_type>, _Alloc =
            std::allocator< boost::tuples:: tuple<uint32_t, boost::shared_p tr<int>,
            boost::tuples:: null_type, boost::tuples:: null_type,
            boost::tuples:: null_type, boost::tuples:: null_type,
            boost::tuples:: null_type, boost::tuples:: null_type,
            boost::tuples:: null_type, boost::tuples:: null_type]
            //end
            shorter:
            std::vector<... >::insert(std:: vector<...>::it erator, const tuple<...>&)
            /usr/include/c++/4.0.0/bits/stl_vector.h:65 7: note:
            void std::vector<_Tp ,
            //beginning
            _Alloc>::insert (__gnu_cxx::__n ormal_iterator< typename _Alloc::pointer ,
            std::vector<_Tp , _Alloc, size_t, const _Tp&) [with _Tp =
            boost::tuples:: tuple<uint32_t, boost::shared_p tr<int>,
            boost::tuples:: null_type, boost::tuples:: null_type,
            boost::tuples:: null_type, boost::tuples:: null_type,
            boost::tuples:: null_type, boost::tuples:: null_type,
            boost::tuples:: null_type, boost::tuples:: null_type>, _Alloc =
            std::allocator< boost::tuples:: tuple<uint32_t, boost::shared_p tr<int>,
            boost::tuples:: null_type, boost::tuples:: null_type,
            boost::tuples:: null_type, boost::tuples:: null_type,
            boost::tuples:: null_type, boost::tuples:: null_type,
            boost::tuples:: null_type, boost::tuples:: null_type]
            //end

            shorter:
            std::vector<... >::insert(std:: vector<...>::it erator, size_t, const
            tuple<...>&)

            Read the messages. They're long and hairy, but it's possible. Matching
            parens is a good start.

            --
            rbh

            Comment

            • Roland Pibinger

              #7
              Re: Some problems with std::vector and nested templates

              On Sun, 17 Jun 2007 15:22:52 -0000, jubelbrus wrote:
              >I get the following error:
              >list.h: In member function 'uint32_t
              >stdtools::list <T>::insert(boo st::shared_ptr< T>) [with T = int]':
              >tester_a.cpp:1 0: instantiated from here
              >list.h:90: error: no matching function for call to
              >'std::vector<b oost::tuples::t uple<uint32_t, boost::shared_p tr<int>,
              >boost::tuples: :null_type, boost::tuples:: null_type,
              >boost::tuples: :null_type, boost::tuples:: null_type,
              >boost::tuples: :null_type, boost::tuples:: null_type,
              >boost::tuples: :null_type, boost::tuples:: null_type>,
              [snipped template jumble]

              Why don't you ask Boost when you've got a Boost problem?



              --
              Roland Pibinger
              "The best software is simple, elegant, and full of drama" - Grady Booch

              Comment

              • John Harrison

                #8
                Re: Some problems with std::vector and nested templates

                >Perhaps you mean
                >>
                > m_vector.push_b ack( add_item );
                >>
                >There is no method of vector called insert that takes a single argument.
                >>
                >john
                >
                oh... :)
                >
                thanks
                >
                Those super long error messages make you think there must be something
                really significant wrong with your code. But sometimes it's just the
                obvious stuff.

                john

                Comment

                • Roland Pibinger

                  #9
                  Re: Some problems with std::vector and nested templates

                  On Sun, 17 Jun 2007 16:00:40 GMT, John Harrison wrote:
                  >Those super long error messages make you think there must be something
                  >really significant wrong with your code. But sometimes it's just the
                  >obvious stuff.
                  Obviously, there is 'something really significant wrong' with Boost.


                  --
                  Roland Pibinger
                  "The best software is simple, elegant, and full of drama" - Grady Booch

                  Comment

                  • Jerry Coffin

                    #10
                    Re: Some problems with std::vector and nested templates

                    In article <4675619b.23636 38@news.utanet. at>, rpbg123@yahoo.c om says...
                    On Sun, 17 Jun 2007 16:00:40 GMT, John Harrison wrote:
                    Those super long error messages make you think there must be something
                    really significant wrong with your code. But sometimes it's just the
                    obvious stuff.
                    >
                    Obviously, there is 'something really significant wrong' with Boost.
                    Hardly -- you can get the same sort of problems without using Boost. The
                    problem is sufficiently widespread that Leor Zolman has put quite a bit
                    of work into stlfilt, to try to make error messages more readable. As I
                    understand it, lousy error messages from templated code also provided
                    the motivation for the work that led to Concepts.

                    --
                    Later,
                    Jerry.

                    The universe is a figment of its own imagination.

                    Comment

                    • John Harrison

                      #11
                      Re: Some problems with std::vector and nested templates

                      Roland Pibinger wrote:
                      On Sun, 17 Jun 2007 16:00:40 GMT, John Harrison wrote:
                      >Those super long error messages make you think there must be something
                      >really significant wrong with your code. But sometimes it's just the
                      >obvious stuff.
                      >
                      Obviously, there is 'something really significant wrong' with Boost.
                      >
                      >
                      At least one compiler I know would quote typedefs that you use back to
                      you in error messages. So if the OP had written

                      typedef typename boost::tuple<ui nt32_t, boost::shared_p tr<T item_type;

                      he might have got a considerably shorter error message.

                      john

                      Comment

                      • Roland Pibinger

                        #12
                        Re: Some problems with std::vector and nested templates

                        On Sun, 17 Jun 2007 11:07:57 -0600, Jerry Coffin wrote:
                        >In article <4675619b.23636 38@news.utanet. at>, rpbg123@...com says...
                        >Obviously, there is 'something really significant wrong' with Boost.
                        >
                        >Hardly -- you can get the same sort of problems without using Boost.
                        Hardly. No one abuses templates the way Boost does.
                        >The
                        >problem is sufficiently widespread that Leor Zolman has put quite a bit
                        >of work into stlfilt, to try to make error messages more readable.
                        The treatment of symptoms is not a cure.
                        >As I
                        >understand it, lousy error messages from templated code also provided
                        >the motivation for the work that led to Concepts.
                        You mean Stroustrup's where-clause? OMG, please no!


                        --
                        Roland Pibinger
                        "The best software is simple, elegant, and full of drama" - Grady Booch

                        Comment

                        • Thomas J. Gritzan

                          #13
                          Re: Some problems with std::vector and nested templates

                          Roland Pibinger wrote:
                          >As I
                          >understand it, lousy error messages from templated code also provided
                          >the motivation for the work that led to Concepts.
                          >
                          You mean Stroustrup's where-clause? OMG, please no!
                          What? Why not?

                          Do you like non-understandable error messages?

                          --
                          Thomas

                          Comment

                          • Jerry Coffin

                            #14
                            Re: Some problems with std::vector and nested templates

                            In article <4675a1c3.10722 21@news.utanet. at>, rpbg123@yahoo.c om says...
                            On Sun, 17 Jun 2007 11:07:57 -0600, Jerry Coffin wrote:
                            In article <4675619b.23636 38@news.utanet. at>, rpbg123@...com says...
                            Obviously, there is 'something really significant wrong' with Boost.
                            Hardly -- you can get the same sort of problems without using Boost.
                            >
                            Hardly. No one abuses templates the way Boost does.
                            Actually, everybody else who wants to accomplish the same sorts of
                            things tends to write fairly similar code -- there are only so many ways
                            to skin a cat...
                            The
                            problem is sufficiently widespread that Leor Zolman has put quite a bit
                            of work into stlfilt, to try to make error messages more readable.
                            >
                            The treatment of symptoms is not a cure.
                            I didn't say it was a cure -- but note the "stl" in its name -- it's for
                            fixing messages that arise from the standard library, NOT Boost.
                            As I
                            understand it, lousy error messages from templated code also provided
                            the motivation for the work that led to Concepts.
                            >
                            You mean Stroustrup's where-clause? OMG, please no!
                            I mean Concepts. For the formal paper see:



                            If you care, you can find quite a bit more at:



                            There will probably be at least a few more minor changes by the time the
                            standard is finalized, but most of it seems nearly certain to be in C++
                            0x.

                            --
                            Later,
                            Jerry.

                            The universe is a figment of its own imagination.

                            Comment

                            Working...