Using class templates Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Iosif
Earned a small fee
Earned a small fee
Posts: 13
Joined: Wed May 02, 2007 7:15 am

Using class templates

Post by Iosif » Wed May 23, 2007 1:38 pm

Hi guys,

I've tried to use templates with wxWidgets and I have a problem.
I defined a class template in a header file (SControlResize.h), like:

Code: Select all

template <
	typename T
>
class SControlResizer : public T
{
	DECLARE_EVENT_TABLE()

public:
	// Mouse left down event
	void	OnMouseLeftDown(wxMouseEvent& event)
	{
		event.Skip();
	}
	// Mouse left up event
	void	OnMouseLeftUp(wxMouseEvent& event)
	{
		event.Skip();
	}
	// Mouse move event
	void	OnMouseMove(wxMouseEvent& event)
	{
		event.Skip();
	}
};
Then I tried to use the one template table event implementation in the .cpp file (ScontrolResize.cpp):

Code: Select all

BEGIN_EVENT_TABLE_TEMPLATE1(SControlResizer, T, T)
	EVT_MOTION(SControlResizer::OnMouseMove)
	EVT_LEFT_UP(SControlResizer::OnMouseLeftUp)
	EVT_LEFT_DOWN(SControlResizer::OnMouseLeftDown)
END_EVENT_TABLE()
And when I compile I got the following errors:
SControlResizer.cpp
D:\wx\Work\testApp60\GUI\wx\Control\SControlResizer.cpp(24) : error C2321: syntax error : unexpected 'SControlResizer<T>::sm_eventTable'
D:\wx\Work\testApp60\GUI\wx\Control\SControlResizer.cpp(24) : error C2063: 'sm_eventHashTable' : not a function
D:\wx\Work\testApp60\GUI\wx\Control\SControlResizer.cpp(24) : error C2040: 'sm_eventHashTable' : 'class wxEventHashTable (void)' differs in levels of indirection from 'class wxEventHashTable'
D:\wx\Work\testApp60\GUI\wx\Control\SControlResizer.cpp(24) : error C2954: template definitions cannot nest

I found out that the problem is in the BEGIN_EVENT_TABLE_TEMPLATE1 macro (wx\event.h) at the following line:
template<typename T1> \
wxEventHashTable theClass<T1>::sm_eventHashTable(theClass<T1>::sm_eventTable); \

and if I change the initialization of 'sm_eventHashTable' variable to use asignment operator instead of parameterized constructor, it works:
template<typename T1> \
wxEventHashTable theClass<T1>::sm_eventHashTable=theClass<T1>::sm_eventTable; \

Do you know why this happens ? How can I fix it without changing the source code (I'm afraid I'll break compatibility with future versions) ?

I use Microsoft Visual C++ 6.0, wxWidgets 2.8.3 debug unicode, windows XP.

Thanks.

phlox81
wxWorld Domination!
wxWorld Domination!
Posts: 1387
Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany
Contact:

Post by phlox81 » Wed May 23, 2007 1:51 pm

you cannot use .cpp files with templates yet, since the export Keyword isn't used by most compilers.

Also MSVC6 isn't the best Compiler for templates, its pretty old.
You should change to a newer Compiler/IDE, like MSVC Express or gcc/Code::Blocks.

swapd0
I live to help wx-kind
I live to help wx-kind
Posts: 169
Joined: Mon May 14, 2007 11:16 am
Location: Spain

Post by swapd0 » Wed May 23, 2007 1:56 pm

Hi, if SControlResizer is a template i think that you must write:

Code: Select all

BEGIN_EVENT_TABLE_TEMPLATE1(SControlResizer<T>, T, T)
        EVT_MOTION(SControlResizer<T>::OnMouseMove)
        EVT_LEFT_UP(SControlResizer<T>::OnMouseLeftUp)
        EVT_LEFT_DOWN(SControlResizer<T>::OnMouseLeftDown)
END_EVENT_TABLE()
 
also, i think that is more correct to define the template with a class not a typename

Code: Select all

template<class T>

class SControlResizer : public T
{
        DECLARE_EVENT_TABLE()

public:
...

Iosif
Earned a small fee
Earned a small fee
Posts: 13
Joined: Wed May 02, 2007 7:15 am

Post by Iosif » Wed May 23, 2007 2:15 pm

phlox81 wrote:you cannot use .cpp files with templates yet, since the export Keyword isn't used by most compilers.

Also MSVC6 isn't the best Compiler for templates, its pretty old.
You should change to a newer Compiler/IDE, like MSVC Express or gcc/Code::Blocks.
If I put the event table definition in the header, I got the same errors.
Yes, I agree with you MSVC6 isn't the best compiler but I choose it because I had some troubles using MSVC Express with wxWidgets library (I got a bunch of link errors when I compiled wxWidgets library). Have you tried to build wxWidgets using MSVC++ 2005 Express Edition ? If yes please tell me if you did some custom settings. I would like to know how to get rid of those link errors.

Thanks.

Iosif
Earned a small fee
Earned a small fee
Posts: 13
Joined: Wed May 02, 2007 7:15 am

Post by Iosif » Wed May 23, 2007 2:20 pm

swapd0 wrote:Hi, if SControlResizer is a template i think that you must write:

Code: Select all

BEGIN_EVENT_TABLE_TEMPLATE1(SControlResizer<T>, T, T)
        EVT_MOTION(SControlResizer<T>::OnMouseMove)
        EVT_LEFT_UP(SControlResizer<T>::OnMouseLeftUp)
        EVT_LEFT_DOWN(SControlResizer<T>::OnMouseLeftDown)
END_EVENT_TABLE()
 
also, i think that is more correct to define the template with a class not a typename

Code: Select all

template<class T>

class SControlResizer : public T
{
        DECLARE_EVENT_TABLE()

public:
...
You're partially right. In the macro BEGIN_EVENT_TABLE_TEMPLATE1 class name SControlResizer must not have <T>, because the macro adds it when needed (check wx\event.h). But in the EVT_MOTION, EVT_LEFT_UP and EVT_LEFT_DOWN the class must be used as SControlResizer<T>. However, this is not the reason of the error messages.
There is no difference between 'template <typename T>' or 'template <class T>'

Thanks.

mispunt
Experienced Solver
Experienced Solver
Posts: 59
Joined: Tue Oct 19, 2004 3:23 pm
Location: Ede, Holland

Post by mispunt » Wed May 23, 2007 2:21 pm

Iosif wrote: Have you tried to build wxWidgets using MSVC++ 2005 Express Edition ? If yes please tell me if you did some custom settings. I would like to know how to get rid of those link errors.
It worked perfectly for my when I used the command line commands.. (wxWidgets 2.8.4)
Edit: when using the command line, you have to make sure that the Platform SDK environment variables are also loaded (the batch file setenv.cmd)
Last edited by mispunt on Wed May 23, 2007 2:22 pm, edited 1 time in total.
OS: win XP pro
Compiler: MingW
wxWidgets version: 2.6.2

phlox81
wxWorld Domination!
wxWorld Domination!
Posts: 1387
Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany
Contact:

Post by phlox81 » Wed May 23, 2007 2:22 pm

I use codeblocks, but building for MSVC Express isn't the big problem I guess.
Also you can download wxPack, and you will an Installer for MSVC Express.

And I am not sure, that you can use an Eventtable with a template.
Maybe you need to use Connect instead.

Iosif
Earned a small fee
Earned a small fee
Posts: 13
Joined: Wed May 02, 2007 7:15 am

Post by Iosif » Wed May 23, 2007 2:23 pm

mispunt wrote:
Iosif wrote: Have you tried to build wxWidgets using MSVC++ 2005 Express Edition ? If yes please tell me if you did some custom settings. I would like to know how to get rid of those link errors.
It worked perfectly for my when I used the command line commands.. (wxWidgets 2.8.4)
That's great. Thanks. Then I will try to use the 2.8.4 version with MSVC Express.

Thanks.

Iosif
Earned a small fee
Earned a small fee
Posts: 13
Joined: Wed May 02, 2007 7:15 am

Post by Iosif » Wed May 23, 2007 2:33 pm

phlox81 wrote:I use codeblocks, but building for MSVC Express isn't the big problem I guess.
Also you can download wxPack, and you will an Installer for MSVC Express.

And I am not sure, that you can use an Eventtable with a template.
Maybe you need to use Connect instead.
Well, I think it's possible to use templates with events, as long as it worked when I changed the definition of the variable 'sm_eventHashTable' from constructor to assign operator. I think that it's a compiler problem and changing it to a newer one will solve the problem.

Thanks a lot guys for your help.

phlox81
wxWorld Domination!
wxWorld Domination!
Posts: 1387
Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany
Contact:

Post by phlox81 » Wed May 23, 2007 4:00 pm

Iosif wrote:
phlox81 wrote:I use codeblocks, but building for MSVC Express isn't the big problem I guess.
Also you can download wxPack, and you will an Installer for MSVC Express.

And I am not sure, that you can use an Eventtable with a template.
Maybe you need to use Connect instead.
Well, I think it's possible to use templates with events, as long as it worked when I changed the definition of the variable 'sm_eventHashTable' from constructor to assign operator. I think that it's a compiler problem and changing it to a newer one will solve the problem.

Thanks a lot guys for your help.
All I said is that you might can't use the Macro eventtable, but Connect still works.

Iosif
Earned a small fee
Earned a small fee
Posts: 13
Joined: Wed May 02, 2007 7:15 am

Post by Iosif » Wed May 23, 2007 7:20 pm

Hi guys,

I've downloaded wxWidgets version 2.8.4 and I compiled it with MSVC Express. Everything was ok and also the sample code compiled successfuly. It was indeed a compiler problem.

Thanks.

Post Reply