Template compilation error (Nested class pointer specific)

K

krunalb

hi,

I am facing compilation error for following snippet:


Compilation Error:
t7.cc:17: error: expected `)' before '*' token


<code snippet>
#include <iostream>

template <typename Element>
class Sort
{
public:
class Info
{
public:
Info() {}
};

public:
=========== Error Line.
Error seems to occur only when we try to use pointer/reference to Info
class. Shockingly, if this pointer is second parameter error vanishes.
(Sort(uint32_t i, Sort<Element>::Info* pInfo) works)
Sort(Sort<Element>::Info* pInfo) {}
};

int main()
{

}
</code snippet>
 
I

Ian Collins

krunalb said:
<code snippet>
#include <iostream>

template <typename Element>
class Sort
{
public:
class Info
{
public:
Info() {}
};

public:
=========== Error Line.
Error seems to occur only when we try to use pointer/reference to Info
class. Shockingly, if this pointer is second parameter error vanishes.
(Sort(uint32_t i, Sort<Element>::Info* pInfo) works)
Sort(Sort<Element>::Info* pInfo) {}

You are missing a typename:

Sort(typename Sort<Element>::Info* pInfo) {}
 
N

Neelesh Bodas

hi,

I am facing compilation error for following snippet:

Compilation Error:
t7.cc:17: error: expected `)' before '*' token

<code snippet>
#include <iostream>

template <typename Element>
class Sort
{
public:
class Info
{
public:
Info() {}
};

public:
=========== Error Line.
Error seems to occur only when we try to use pointer/reference to Info
class. Shockingly, if this pointer is second parameter error vanishes.
(Sort(uint32_t i, Sort<Element>::Info* pInfo) works)
Sort(Sort<Element>::Info* pInfo) {}

Sort(typename Sort<Element>::Info* pInfo) {}

BTW no need to specify the template parameter and the name of the
enclosing class.
i.e. Sort(Info* pInfo) {}
will also work.

-N
 
G

Gianni Mariani

krunalb said:
hi,

I am facing compilation error for following snippet:


Compilation Error:
t7.cc:17: error: expected `)' before '*' token


<code snippet>
#include <iostream>

template <typename Element>
class Sort
{
public:
class Info
{
public:
Info() {}
};

public:
=========== Error Line.
Error seems to occur only when we try to use pointer/reference to Info
class. Shockingly, if this pointer is second parameter error vanishes.
(Sort(uint32_t i, Sort<Element>::Info* pInfo) works)
Sort(Sort<Element>::Info* pInfo) {}

This works:
Sort( typename Sort<Element>::Info * pInfo) {}

This works too:
Sort( Info * pInfo) {}

.... In general, you need to use "typename" when spefifying a type like
you have unless it is already specified as type (like class Info).
 

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