How to call a template function in a template class?

Discussion in 'C++' started by Gawain, Jan 12, 2008.

  1. Gawain

    Gawain Guest

    Hi Guys,

    I am trying to call a template function in a template class like
    below, but compiling error occured. Do anyone know how to solve it?
    thanks.

    class A {
    public:
    template<bool b> void foo() {; }
    };

    template<class T >
    class B {
    public:
    void foo() {
    T t;
    t.foo<false> (); // error occured here.
    }
    };

    main()
    {
    B<A> val;
    val.foo();
    }


    Error details:
    ==============================================
    Test.cpp: In member function `void B<T>::foo()':
    Test.cpp:26: error: expected primary-expression before ')' token
    Test.cpp: In member function `void B<T>::foo() [with T = A]':
    Test.cpp:33: instantiated from here
    Test.cpp:26: error: invalid use of member (did you forget the `&' ?)
     
    Gawain, Jan 12, 2008
    #1
    1. Advertising

  2. Gawain

    Rolf Magnus Guest

    Gawain wrote:

    > Hi Guys,
    >
    > I am trying to call a template function in a template class like
    > below, but compiling error occured. Do anyone know how to solve it?
    > thanks.
    >
    > class A {
    > public:
    > template<bool b> void foo() {; }
    > };
    >
    > template<class T >
    > class B {
    > public:
    > void foo() {
    > T t;
    > t.foo<false> (); // error occured here.


    t.template foo<false> ();

    > }
    > };
    >
    > main()


    Function without a return type are not allowed in C++. Further, the return
    type of main must always be int.

    > {
    > B<A> val;
    > val.foo();
    > }
     
    Rolf Magnus, Jan 12, 2008
    #2
    1. Advertising

  3. Gawain

    Gawain Guest

    On Jan 12, 6:18 pm, Rolf Magnus <> wrote:
    > Gawain wrote:
    > > Hi Guys,

    >
    > > I am trying to call a template function in a template class like
    > > below, but compiling error occured. Do anyone know how to solve it?
    > > thanks.

    >
    > > class A {
    > > public:
    > >     template<bool b>  void foo() {;  }
    > > };

    >
    > > template<class T >
    > > class B {
    > > public:
    > >     void foo()   {
    > >         T t;
    > >         t.foo<false> ();    // error occured here.

    >
    >           t.template foo<false> ();
    >
    > >     }
    > > };

    >
    > > main()

    >
    > Function without a return type are not allowed in C++. Further, the return
    > type of main must always be int.
    >
    >
    >
    > > {
    > >     B<A> val;
    > >     val.foo();
    > > }- Hide quoted text -

    >
    > - Show quoted text -- Hide quoted text -
    >
    > - Show quoted text -


    Sorry, it's not the problem.
    the key problem seems like C++ compiler do not know that A.foo() is a
    template function member.
     
    Gawain, Jan 12, 2008
    #3
  4. Gawain

    Guest

    Gawain wrote:

    > Hi Guys,
    >
    > I am trying to call a template function in a template class like
    > below, but compiling error occured. Do anyone know how to solve it?
    > thanks.
    >
    > class A {
    > public:
    > template<bool b> void foo() {; }
    > };
    >
    > template<class T >
    > class B {
    > public:
    > void foo() {
    > T t;
    > t.foo<false> (); // error occured here.


    I think the dependent name foo is not recognized as a template. Maybe
    something like

    t.template foo<false> ();

    will work.

    > }
    > };
    >
    > main()


    int main ()

    > {
    > B<A> val;
    > val.foo();
    > }

    [snip]

    Best

    Kai-Uwe Bux
     
    , Jan 12, 2008
    #4
  5. Gawain

    Rolf Magnus Guest

    Gawain wrote:

    > On Jan 12, 6:18 pm, Rolf Magnus <> wrote:
    >> Gawain wrote:
    >> > Hi Guys,

    >>
    >> > I am trying to call a template function in a template class like
    >> > below, but compiling error occured. Do anyone know how to solve it?
    >> > thanks.

    >>
    >> > class A {
    >> > public:
    >> > template<bool b>  void foo() {;  }
    >> > };

    >>
    >> > template<class T >
    >> > class B {
    >> > public:
    >> > void foo()   {
    >> > T t;
    >> > t.foo<false> ();    // error occured here.

    >>
    >> t.template foo<false> ();
    >>
    >> > }
    >> > };

    >>
    >> > main()

    >>
    >> Function without a return type are not allowed in C++. Further, the
    >> return type of main must always be int.
    >>
    >>
    >>
    >> > {
    >> > B<A> val;
    >> > val.foo();
    >> > }

    >
    > Sorry, it's not the problem.
    > the key problem seems like C++ compiler do not know that A.foo() is a
    > template function member.


    If your compiler still doesn't know it with my suggestion, it is probably
    broken.
     
    Rolf Magnus, Jan 12, 2008
    #5
  6. On 2008-01-12 11:23, Gawain wrote:
    > On Jan 12, 6:18 pm, Rolf Magnus <> wrote:
    >> Gawain wrote:
    >> > Hi Guys,

    >>
    >> > I am trying to call a template function in a template class like
    >> > below, but compiling error occured. Do anyone know how to solve it?
    >> > thanks.

    >>
    >> > class A {
    >> > public:
    >> > template<bool b> void foo() {; }
    >> > };

    >>
    >> > template<class T >
    >> > class B {
    >> > public:
    >> > void foo() {
    >> > T t;
    >> > t.foo<false> (); // error occured here.

    >>
    >> t.template foo<false> ();
    >>
    >> > }
    >> > };

    >>
    >> > main()

    >>
    >> Function without a return type are not allowed in C++. Further, the return
    >> type of main must always be int.
    >>
    >>
    >>
    >> > {
    >> > B<A> val;
    >> > val.foo();
    >> > }- Hide quoted text -

    >>
    >> - Show quoted text -- Hide quoted text -
    >>
    >> - Show quoted text -

    >
    > Sorry, it's not the problem.
    > the key problem seems like C++ compiler do not know that A.foo() is a
    > template function member.


    Using "t.template foo<false> ();" and "int main()" should work. However
    if it still does not work it might be because you did not post the
    actual code you are using (there are less then 26 lines in the code you
    posted but the error-message mentions line 26). Please read the FAQ 5.8
    (http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.8) and try
    again.

    --
    Erik Wikström
     
    Erik Wikström, Jan 12, 2008
    #6
  7. Gawain

    Gawain Guest

    On Jan 12, 8:34 pm, Erik Wikström <> wrote:
    > On 2008-01-12 11:23, Gawain wrote:
    >
    >
    >
    >
    >
    > > On Jan 12, 6:18 pm, Rolf Magnus <> wrote:
    > >> Gawain wrote:
    > >> > Hi Guys,

    >
    > >> > I am trying to call a template function in a template class like
    > >> > below, but compiling error occured. Do anyone know how to solve it?
    > >> > thanks.

    >
    > >> > class A {
    > >> > public:
    > >> >     template<bool b>  void foo() {;  }
    > >> > };

    >
    > >> > template<class T >
    > >> > class B {
    > >> > public:
    > >> >     void foo()   {
    > >> >         T t;
    > >> >         t.foo<false> ();    // error occured here.

    >
    > >>           t.template foo<false> ();

    >
    > >> >     }
    > >> > };

    >
    > >> > main()

    >
    > >> Function without a return type are not allowed in C++. Further, the return
    > >> type of main must always be int.

    >
    > >> > {
    > >> >     B<A> val;
    > >> >     val.foo();
    > >> > }- Hide quoted text -

    >
    > >> - Show quoted text -- Hide quoted text -

    >
    > >> - Show quoted text -

    >
    > > Sorry, it's not the problem.
    > > the key problem seems like C++ compiler do not know that A.foo() is a
    > > template function member.

    >
    > Using "t.template foo<false> ();" and "int main()" should work. However
    > if it still does not work it might be because you did not post the
    > actual code you are using (there are less then 26 lines in the code you
    > posted but the error-message mentions line 26). Please read the FAQ 5.8
    > (http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.8) and try
    > again.
    >
    > --
    > Erik Wikström- Hide quoted text -
    >
    > - Show quoted text -


    Issue solved by using statement "t.template foo<false> (); "
    Thanks all, especially Kai-Uwe and Erik's kindly help.
     
    Gawain, Jan 12, 2008
    #7
  8. Gawain

    Rahul Guest

    On Jan 12, 3:52 pm, wrote:
    > Gawain wrote:
    > > Hi Guys,

    >
    > > I am trying to call a template function in a template class like
    > > below, but compiling error occured. Do anyone know how to solve it?
    > > thanks.

    >
    > > class A {
    > > public:
    > > template<bool b> void foo() {; }
    > > };

    >
    > > template<class T >
    > > class B {
    > > public:
    > > void foo() {
    > > T t;
    > > t.foo<false> (); // error occured here.

    >
    > I think the dependent name foo is not recognized as a template. Maybe
    > something like
    >
    > t.template foo<false> ();
    >
    > will work.
    >
    > > }
    > > };

    >
    > > main()

    >
    > int main ()
    >
    > > {
    > > B<A> val;
    > > val.foo();
    > > }

    >
    > [snip]
    >
    > Best
    >
    > Kai-Uwe Bux


    I tried t.template foo<false> (); in MS VC++, but it gives this error,

    template declarations are only permitted at global or namespace scope

    any idea?

    Thanks in advance!!!
     
    Rahul, Jan 13, 2008
    #8
  9. Gawain

    Guest

    Rahul wrote:

    > On Jan 12, 3:52 pm, wrote:
    >> Gawain wrote:
    >> > Hi Guys,

    >>
    >> > I am trying to call a template function in a template class like
    >> > below, but compiling error occured. Do anyone know how to solve it?
    >> > thanks.

    >>
    >> > class A {
    >> > public:
    >> > template<bool b> void foo() {; }
    >> > };

    >>
    >> > template<class T >
    >> > class B {
    >> > public:
    >> > void foo() {
    >> > T t;
    >> > t.foo<false> (); // error occured here.

    >>
    >> I think the dependent name foo is not recognized as a template. Maybe
    >> something like
    >>
    >> t.template foo<false> ();
    >>
    >> will work.
    >>
    >> > }
    >> > };

    >>
    >> > main()

    >>
    >> int main ()
    >>
    >> > {
    >> > B<A> val;
    >> > val.foo();
    >> > }

    >>
    >> [snip]
    >>
    >> Best
    >>
    >> Kai-Uwe Bux

    >
    > I tried t.template foo<false> (); in MS VC++, but it gives this error,
    >
    > template declarations are only permitted at global or namespace scope
    >
    > any idea?


    No.

    As far as I can see, the following is standard compliant code and should be
    accepted by any compliant compiler:

    struct A {
    template <bool b>
    void foo () {}
    };

    template<class T >
    struct B {
    void foo() {
    T t;
    t.template foo<false> ();
    }
    };

    int main() {
    B<A> val;
    val.foo();
    }

    If MS VC++ does not compile it, you might consider filing a bug report.


    Best

    Kai-Uwe Bux
     
    , Jan 13, 2008
    #9
  10. Gawain

    red floyd Guest

    Rahul wrote:

    >
    > I tried t.template foo<false> (); in MS VC++, but it gives this error,
    >
    > template declarations are only permitted at global or namespace scope
    >
    > any idea?
    >
    > Thanks in advance!!!


    Which version? If it's VC6, forget it. It's pre-standard and known to
    have horrible template support. VC7.1 (2003) and VC8 (2005) track the
    Standard fairly well (except for export).
     
    red floyd, Jan 14, 2008
    #10
    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. christopher diggins
    Replies:
    16
    Views:
    756
    Pete Becker
    May 4, 2005
  2. George2
    Replies:
    0
    Views:
    355
    George2
    Mar 17, 2008
  3. Replies:
    2
    Views:
    543
  4. A L
    Replies:
    1
    Views:
    510
    Alf P. Steinbach /Usenet
    Aug 25, 2010
  5. THAKUR PRASHANT SINGH

    Class Function call vs Normal Function call

    THAKUR PRASHANT SINGH, Feb 26, 2010, in forum: Ruby
    Replies:
    7
    Views:
    195
    THAKUR PRASHANT SINGH
    Feb 27, 2010
Loading...

Share This Page