How to call a template function in a template class?

G

Gawain

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 `&' ?)
 
R

Rolf Magnus

Gawain said:
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 said:
}
};

main()

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

Gawain

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




- 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.
 
J

jkherciueh

Gawain said:
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
 
R

Rolf Magnus

Gawain said:
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.
 
E

Erik Wikström

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.
 
G

Gawain

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.

Issue solved by using statement "t.template foo<false> (); "
Thanks all, especially Kai-Uwe and Erik's kindly help.
 
R

Rahul

Gawain said:
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.

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!!!
 
J

jkherciueh

Rahul said:
Gawain said:
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.

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
 
R

red floyd

Rahul said:
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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top