compilation error with templates

Discussion in 'C++' started by Marc.Tajchman@gmail.com, Jul 22, 2005.

  1. Guest

    Hi everybody,

    Here is a simple C++ example :

    1 #include <vector>
    2
    3 template <typename T>
    4 class X {
    5 T _t;
    6 public :
    7 X(const T & t) : _t(t) {}
    8 };
    9
    10 template <typename U >
    11 void f(U t)
    12 {
    13 std::vector< X<U> > v;
    14 std::vector< X<U> >::iterator iv;
    15 }
    16
    17 int main()
    18 {
    19 std::vector<int>::iterator i;
    20 std::vector< X<float> >::iterator j;
    21 return 0;
    22 }

    Line 14 gives me the following compiler error (g++ 3.4.1) :
    ----------------------------------------------------------
    test.cxx: In function `void f(U)':
    test.cxx:14: error: expected `;' before "iv"
    ----------------------------------------------------------
    but line 20 is accepted.

    Could you tell me what's wrong (probably a simple syntax problem,
    but I don't see which one) ?

    Thanks in advance.

    Marc
     
    , Jul 22, 2005
    #1
    1. Advertising

  2. wrote:
    > Hi everybody,
    >
    > Here is a simple C++ example :
    >
    > 1 #include <vector>
    > 2
    > 3 template <typename T>
    > 4 class X {
    > 5 T _t;
    > 6 public :
    > 7 X(const T & t) : _t(t) {}
    > 8 };
    > 9
    > 10 template <typename U >
    > 11 void f(U t)
    > 12 {
    > 13 std::vector< X<U> > v;
    > 14 std::vector< X<U> >::iterator iv;
    > 15 }
    > 16
    > 17 int main()
    > 18 {
    > 19 std::vector<int>::iterator i;
    > 20 std::vector< X<float> >::iterator j;
    > 21 return 0;
    > 22 }
    >
    > Line 14 gives me the following compiler error (g++ 3.4.1) :
    > ----------------------------------------------------------
    > test.cxx: In function `void f(U)':
    > test.cxx:14: error: expected `;' before "iv"
    > ----------------------------------------------------------
    > but line 20 is accepted.
    >
    > Could you tell me what's wrong (probably a simple syntax problem,
    > but I don't see which one) ?


    Read about "dependent name" in the FAQ.

    And in the future posts, please don't put numbers next to every line,
    put the number of a line of interest in a comment in the same line.

    V
     
    Victor Bazarov, Jul 22, 2005
    #2
    1. Advertising

  3. Alipha Guest

    > std::vector< X<U> >::iterator iv;

    Here, std::vector can't be instantiated until f is used and U is known.
    Since the std::vector template isn't instantiated, the compiler can't
    know anything about the members of the std::vector class (namely
    because of template specialization,) and so, is iterator a typename or
    a static member? the compiler can't know and assumes the latter. you
    need to tell it otherwise:

    typename std::vector< X<U> >::iterator iv;

    the template keyword can also be used in contexts such as these to
    relieve ambiguity:

    template<class U>
    void f() {
    typename std::vector<U>::allocator_type::template rebind<long>::eek:ther
    long_allocator;
    }

    ...to give an impractical example.
     
    Alipha, Jul 22, 2005
    #3
  4. benben Guest


    > Hi everybody,
    >
    > Here is a simple C++ example :
    >
    > 1 #include <vector>
    > 2
    > 3 template <typename T>
    > 4 class X {
    > 5 T _t;
    > 6 public :
    > 7 X(const T & t) : _t(t) {}
    > 8 };
    > 9
    > 10 template <typename U >
    > 11 void f(U t)
    > 12 {
    > 13 std::vector< X<U> > v;
    > 14 std::vector< X<U> >::iterator iv;
    > 15 }
    > 16
    > 17 int main()
    > 18 {
    > 19 std::vector<int>::iterator i;
    > 20 std::vector< X<float> >::iterator j;
    > 21 return 0;
    > 22 }
    >
    > Line 14 gives me the following compiler error (g++ 3.4.1) :
    > ----------------------------------------------------------
    > test.cxx: In function `void f(U)':
    > test.cxx:14: error: expected `;' before "iv"
    > ----------------------------------------------------------
    > but line 20 is accepted.
    >
    > Could you tell me what's wrong (probably a simple syntax problem,
    > but I don't see which one) ?
    >
    > Thanks in advance.
    >
    > Marc
    >


    1. Please don't add line numbers. It is hard to copy your code and give a
    quick compilation because we have to manually delete those line numbers.
    Simply comment at the line that's causing problems.

    2. Visual C++ compiled the code fine. This could be an accidant.

    3. g++ compiled the code fine if you add a keywork 'typename' before
    std::vector<X<float> >. Why? Because it doesn't know if std::vector<X<float>
    >::iterator is a type name or a member variable.


    Regards,
    Ben
     
    benben, Jul 22, 2005
    #4
  5. Guest

    It works !!

    Thanks very much for all the answers and sorry for putting
    line numbers at an inconvenient posision.

    Marc
     
    , Jul 25, 2005
    #5
    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. Rolf Magnus
    Replies:
    1
    Views:
    317
    tom_usenet
    Jul 7, 2003
  2. JKop
    Replies:
    3
    Views:
    521
  3. Generic Usenet Account
    Replies:
    3
    Views:
    849
    Generic Usenet Account
    Jul 14, 2005
  4. recover
    Replies:
    2
    Views:
    857
    recover
    Jul 25, 2006
  5. C__chp
    Replies:
    4
    Views:
    542
    Puppet_Sock
    Feb 15, 2008
Loading...

Share This Page