Wednesday 13 June 2018

c++ - Compiling error on template method, return is instance from inner class




Here is a simplified example:



template

class MyTemplate
{
class Inner {};
Inner met();
};

template
MyTemplate::Inner MyTemplate::met()
{ }



I get the following compilation error:



expected constructor, destructor, or type conversion before 'met'


I use GCC. It seems the compiler doesn't recognize MyTemplate::Inner as a proper class. How can I fix this? I've tried sticking the typename keyword here and there to no avail. Right now, the only way I can manage to compile this is to inline the method definition in the class declaration, which I would like to avoid.


Answer



Clang reports the following:




error: missing 'typename' prior to dependent type name
'MyTemplate::Inner' MyTemplate::Inner MyTemplate::met()
^~~~~~~~~~~~~~~~~~~~ typename 1 error generated.


And placing typename in the appropriate place fixes it.



template
class MyTemplate
{

class Inner {};
Inner met();
};

template
typename MyTemplate::Inner MyTemplate::met()
{ }


Did you put typename in the correct location? If so, then this must be a bug in G++.



No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...