LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-25-2004, 07:53 PM   #1
ckcheung0927
LQ Newbie
 
Registered: Nov 2004
Posts: 1

Rep: Reputation: 0
Unhappy template class


Dear All,

I have problem on programming in template class.
Code:
#include <stdio.h>
template <class ArgType>
class ClassA
{
public:
	template <class ClsType>
		void Func( ClsType* pObj, void (ClsType::*pClsFunc)(ArgType), ArgType arg )
	{
		(pObj->*pClsFunc)( arg );
	}	
};

class ClassB
{
public:
	void Func( int a )
	{
		printf( "a=%d\n", a );
	}
};

int main( int argc, char* argv[] )
{
	ClassA<int> a;
	ClassB b;

	a.Func( &b, ClassB::Func, 1 );

	return 0;
}
while i use g++ to compile the program, i got the following message

test.cpp: In function `int main(int, char**)':
test.cpp:28: no matching function for call to `ClassA<int>::Func(ClassB*,
<unknown type>, int)'
test.cpp:9: candidates are: void ClassA<ArgType>::Func(ClsType*, void
(ClsType::*)(ArgType), ArgType) [with ClsType = ClassB, ArgType = int]


But this program can be successfully compiled in VC++.

Please Help.

Reply me please

Last edited by ckcheung0927; 11-25-2004 at 08:01 PM.
 
Old 11-26-2004, 01:03 PM   #2
MartinN
Member
 
Registered: Nov 2003
Location: Ronneby, Sweden
Posts: 555

Rep: Reputation: 30
You need to send a function pointer as argument in main. It should look like this:
Code:
int main( int argc, char* argv[] )
{
	ClassA<int> a;
	ClassB b;

	a.Func( &b, &ClassB::Func, 1 );
	// Note the '&' before ClassB::Func

	return 0;
}
Martin
 
Old 11-26-2004, 01:51 PM   #3
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 475Reputation: 475Reputation: 475Reputation: 475Reputation: 475
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 11-28-2004, 03:59 PM   #4
ashirazi
Member
 
Registered: Jul 2004
Posts: 60

Rep: Reputation: 15
looks like you're kinda new to templates. I use this tutorial which helped me.

[LINK]http://babbage.cs.qc.edu/STL_Docs/templates.htm[/LINK]

raven
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
c++ class template doesn't work!! tagigogo Programming 2 11-02-2005 04:45 PM
Template class with a template member... Nicholas Bishop Programming 3 02-21-2005 08:27 PM
function prototypes in template class' (c++) qwijibow Programming 4 12-13-2004 09:34 AM
C++ - class template - linker errors? Wynd Programming 2 10-19-2004 03:54 PM
C++ template class syntax rong Programming 12 08-13-2003 12:26 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration