Thread: [Dev-C++] templates and inheritance
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Astar <As...@tu...> - 2003-04-28 22:02:51
|
Hello Dev-Cpp,
I have some problems with using templates.
i have template like:
template <class T, int optimum>
class CConteiner {
.......
};
and i want to make child from it. something like:
CScene{.....};
class CScenes:public CConteiner<CScene*, 4>{
........}
its ok, when i compile it, but when i try to link it, i have error.
something like:
unresolved link to CConteiner<CScene*, 4>::CConteiner()
i tried to change code to:
template <class T>
class CConteiner {
public:
CConteiner(int optimum);
....
};
template <int optim>
class CScenes:CConteiner<CScene*>{
public:
CScenes();
.....
};
template <int optim>
CScenes<optim>::CScenes(): CConteiner<CScenes*>( optim ){
...
}
but linked error appeared again.... (compiling - ok)
How i can inherit templates like this?
any ideas?
thanx a lot....
PS. source code is approximite, couse i'm trying another ways (
aggregation for example, but it's bad decision)
--
Best regards,
Astar mailto:As...@tu...
|
|
From: Ioannis V. <iv...@at...> - 2003-04-29 04:26:11
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf Of Astar
> Sent: Tuesday, April 29, 2003 1:02 AM
> To: Dev-Cpp mail list
> Subject: [Dev-C++] templates and inheritance
>
>
> Hello Dev-Cpp,
>
> I have some problems with using templates.
>
> i have template like:
>
> template <class T, int optimum>
> class CConteiner {
> .......
> };
>
> and i want to make child from it. something like:
>
> CScene{.....};
> class CScenes:public CConteiner<CScene*, 4>{
> ........}
>
> its ok, when i compile it, but when i try to link it, i have error.
> something like:
> unresolved link to CConteiner<CScene*, 4>::CConteiner()
This code compiles. Find the differences. :)
template <class T, int optimum>
class CConteiner
{
};
class CScene{};
class CScenes:public CConteiner<CScene*, 4>
{
};
int main()
{
}
Ioannis Vranos
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net
Anti-racism movement: http://www.positive-youthfoundation.org
Iraq Peace Pledge: http://www.peacepledge.org
|
|
From: Astar <As...@tu...> - 2003-04-29 20:57:14
|
Hello Ioannis,
IV> This code compiles. Find the differences. :)
IV> template <class T, int optimum>
IV> class CConteiner
IV> {
IV> };
IV> class CScene{};
IV> class CScenes:public CConteiner<CScene*, 4>
IV> {
IV> };
IV> int main()
IV> {
IV> }
:)))) very interesting... of course i have main-function (my project
consist of ~40-45 files and i thought that writing about my
main-function (and other parts of project) is unnecessary)
i find problem (it isn't absence of main-function :)).
I have problem with writing implementation of methods
(constructor/destructor too) in two files. I mean, if i write it only
in .h file:
template <class T, int o>
class CConteiner{
CConteiner(){....}
~CConteiner(){....};
};
it's OK!(compiling and linking) ....... but if i write implementation
of constructor/destructor in .cpp file i have link error. I write:
template <class T, int o>
CConteiner<T, optimum>::CConteiner(){
.......
}
template <class T, int o>
CConteiner<T, optimum>::~CConteiner(){
.......
}
what's wrong with my code???
or may be i must write body of methods in declaration?
PS. sorry for my english :)
--
Best regards,
Astar mailto:As...@tu...
|
|
From: Ioannis V. <iv...@at...> - 2003-04-30 00:54:15
|
> -----Original Message-----
> From: dev...@li...=20
> [mailto:dev...@li...] On Behalf Of Astar
> Sent: Tuesday, April 29, 2003 11:56 PM
> To: Dev-Cpp mail list
> Subject: Re[2]: [Dev-C++] templates and inheritance
>=20
> :)))) very interesting... of course i have main-function (my project
> consist of ~40-45 files and i thought that writing about my
> main-function (and other parts of project) is unnecessary)
>=20
> i find problem (it isn't absence of main-function :)).
>=20
> I have problem with writing implementation of methods
> (constructor/destructor too) in two files. I mean, if i write it only
> in .h file:
> template <class T, int o>
> class CConteiner{
> CConteiner(){....}
> ~CConteiner(){....};
> };
>=20
> it's OK!(compiling and linking) ....... but if i write implementation
> of constructor/destructor in .cpp file i have link error. I write:
>=20
> template <class T, int o>
> CConteiner<T, optimum>::CConteiner(){
> .......
> }
>=20
> template <class T, int o>
> CConteiner<T, optimum>::~CConteiner(){
> .......
> }
>=20
> what's wrong with my code???
> or may be i must write body of methods in declaration?
>=20
>=20
> PS. sorry for my english :)
The definitions of template functions and classes must be provided at =
every
file you use them. If you wish to place the definition of the template =
in
one implementation file, you have to use the keyword export. Example:
export template<class T>
class whatever
{
// ...
};
However its implementation is very tough, and not many compilers support =
the
keyword "export".
Ioannis Vranos
=20
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net
Anti-racism movement: http://www.positive-youthfoundation.org
Iraq Peace Pledge: http://www.peacepledge.org
|
|
From: Astar <As...@tu...> - 2003-04-30 13:22:42
|
Hello Ioannis,
IV> The definitions of template functions and classes must be provided at every
IV> file you use them. If you wish to place the definition of the template in
IV> one implementation file, you have to use the keyword export. Example:
IV> export template<class T>
IV> class whatever
IV> {
IV> // ...
IV> };
may be i don't understand you...
i understand that if i have two files (.h/.cpp) i must dublicate
definition of template in .cpp file with word "extern". Am I right?
or i must dublicate definition of template in every file where i use
it?
thanx a lot....
IV> However its implementation is very tough, and not many compilers support the
IV> keyword "export".
--
Best regards,
Astar mailto:As...@tu...
|
|
From: Ioannis V. <iv...@at...> - 2003-04-30 13:30:30
|
> -----Original Message-----
> From: Astar [mailto:As...@tu...]
> Sent: Wednesday, April 30, 2003 4:21 PM
> To: Ioannis Vranos; Dev-Cpp mail list
> Subject: Re[4]: [Dev-C++] templates and inheritance
>
>
> Hello Ioannis,
>
>
> IV> export template<class T>
> IV> class whatever
> IV> {
> IV> // ...
> IV> };
>
> may be i don't understand you...
> i understand that if i have two files (.h/.cpp) i must dublicate
> definition of template in .cpp file with word "extern". Am I right?
> or i must dublicate definition of template in every file where i use
> it?
You can place the definition in one .cpp file accompanied with the keyword
"export" and its *declaration* in every scope you use the class (usually
using a header file),
OR you can place the *definition* in every scope you use the class (e.g.
using a header file) without the keyword "export" of course.
The keyword export requires serious effort to be implemented, so it is not
supported by most compilers (and there is discussion inside the C++
committee for the upcoming C++0x standard, whether the keyword "export" to
become optional.).
Ioannis Vranos
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net
Anti-racism movement: http://www.positive-youthfoundation.org
Iraq Peace Pledge: http://www.peacepledge.org
|