Problem with inner classes and Templates

A

alexroat

Hi, when I'm trying to compile this code with gcc 4 I get this strange
error :

alex@deimos:~/Desktop/Image$ make -k Image
g++ Image.cpp -o Image
Image.cpp:29: error: expected initializer before 'Image'
make: *** [Image] Error 1


The code of the source of Image.cpp is the following:

#include <iostream>

template <class T>
class Image
{
public:

class Handler
{
public:
private:
Handler(class Image &i);
friend class Image;
};


const Handler operator()();

private:


};





template <class T>
const Image<T>::Handler Image<T>::eek:perator()()
{
return Handler(*this);
}


template <class T>
Image<T>::Handler::Handler(class Image<T> &i)
{

}



typedef unsigned char byte;

int main()
{
Image <byte> i1;


}



The problem seems to be not present if I remove the template leaving
as a common class the Image one. I think it's due by the fact that
compiler cannot predict the form of Handler before it has not been
istantiated ... but I'm not sure ... could you help me ?

Is a solution to templatizate also the Handler class and moreover move
it outside the Image class ?

Thank you in advance.

Alessandro
 
A

alexroat

Hi, when I'm trying to compile this code with gcc 4 I get this strange
error :

alex@deimos:~/Desktop/Image$ make -k Image
g++ Image.cpp -o Image
Image.cpp:29: error: expected initializer before 'Image'
make: *** [Image] Error 1

The code of the source of Image.cpp is the following:

#include <iostream>

template <class T>
class Image
{
public:

class Handler
{
public:
private:
Handler(class Image &i);
friend class Image;
};

const Handler operator()();

private:

};

template <class T>
const Image<T>::Handler Image<T>::eek:perator()()
{
return Handler(*this);

}

template <class T>
Image<T>::Handler::Handler(class Image<T> &i)
{

}

typedef unsigned char byte;

int main()
{
Image <byte> i1;

}

The problem seems to be not present if I remove the template leaving
as a common class the Image one. I think it's due by the fact that
compiler cannot predict the form of Handler before it has not been
istantiated ... but I'm not sure ... could you help me ?

Is a solution to templatizate also the Handler class and moreover move
it outside the Image class ?

Thank you in advance.

Alessandro

In declaration is missing <T> after Image, but I get the same error...
could you help me ?
 
A

alexroat

* (e-mail address removed):




Add 'typename'.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Where I have to put it ? Is it not the same as "class" keyword ?
Could you write me the correct code ?

Thank you
 
M

Michael

Hi, when I'm trying to compile this code with gcc 4 I get this strange
error :

alex@deimos:~/Desktop/Image$ make -k Image
g++ Image.cpp -o Image
Image.cpp:29: error: expected initializer before 'Image'
make: *** [Image] Error 1

template <class T>
const Image<T>::Handler Image<T>::eek:perator()()
{
return Handler(*this);

}

Here's VS output. It seems more comprehensible to me:
1>image.cpp(30) : warning C4346: 'Image<T>::Handler' : dependent name
is not a type
1> prefix with 'typename' to indicate a type
1>image.cpp(30) : error C2143: syntax error : missing ';' before
'Image<T>::()'
1>image.cpp(30) : error C4430: missing type specifier - int assumed.
Note: C++ does not support default-int

So basically,
const Image<T>::Handler Image<T>::eek:perator()()
should be
const typename Image<T>::Handler Image<T>::eek:perator()()

It then compiles for me with VS & g++.

Michael
 
P

Piyo

Here's your solution. Enjoy!
(Missing keyword typename)

template <class T>
const typename Image<T>::Handler Image<T>::eek:perator()()
{
return Handler(*this);

}

Hi, when I'm trying to compile this code with gcc 4 I get this strange
error :

alex@deimos:~/Desktop/Image$ make -k Image
g++ Image.cpp -o Image
Image.cpp:29: error: expected initializer before 'Image'
make: *** [Image] Error 1

The code of the source of Image.cpp is the following:

#include <iostream>

template <class T>
class Image
{
public:

class Handler
{
public:
private:
Handler(class Image &i);
friend class Image;
};

const Handler operator()();

private:

};

template <class T>
const Image<T>::Handler Image<T>::eek:perator()()
{
return Handler(*this);

}

template <class T>
Image<T>::Handler::Handler(class Image<T> &i)
{

}

typedef unsigned char byte;

int main()
{
Image <byte> i1;

}

The problem seems to be not present if I remove the template leaving
as a common class the Image one. I think it's due by the fact that
compiler cannot predict the form of Handler before it has not been
istantiated ... but I'm not sure ... could you help me ?

Is a solution to templatizate also the Handler class and moreover move
it outside the Image class ?

Thank you in advance.

Alessandro

In declaration is missing <T> after Image, but I get the same error...
could you help me ?
 
A

alexroat

Here's your solution. Enjoy!
(Missing keyword typename)

template <class T>
const typename Image<T>::Handler Image<T>::eek:perator()()
{
return Handler(*this);

}
Hi, when I'm trying to compile this code with gcc 4 I get this strange
error :
alex@deimos:~/Desktop/Image$ make -k Image
g++ Image.cpp -o Image
Image.cpp:29: error: expected initializer before 'Image'
make: *** [Image] Error 1
The code of the source of Image.cpp is the following:
#include <iostream>
template <class T>
class Image
{
public:
class Handler
{
public:
private:
Handler(class Image &i);
friend class Image;
};
const Handler operator()();
private:
};
template <class T>
const Image<T>::Handler Image<T>::eek:perator()()
{
return Handler(*this);
}
template <class T>
Image<T>::Handler::Handler(class Image<T> &i)
{
}
typedef unsigned char byte;
int main()
{
Image <byte> i1;
}
The problem seems to be not present if I remove the template leaving
as a common class the Image one. I think it's due by the fact that
compiler cannot predict the form of Handler before it has not been
istantiated ... but I'm not sure ... could you help me ?
Is a solution to templatizate also the Handler class and moreover move
it outside the Image class ?
Thank you in advance.
Alessandro
In declaration is missing <T> after Image, but I get the same error...
could you help me ?

WOW !!!
Fantastic !!!
Thank you ...

I'll offer you a beer :-D

CIAO
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top