LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 04-09-2008, 10:58 AM   #1
matze
Member
 
Registered: Oct 2003
Posts: 50

Rep: Reputation: 15
c++: template member function used in a different templated class


Hi,

I've a problem getting the following code to compile (boiled down to the minimum):

Code:
#include <iostream>

class Foo {
public:
  Foo() {};
  
  template<typename T>
  void print() const {
    T con;
    std::cout << "Foo! " << con << std::endl;
  };
};

template<typename S, typename T>
class op : public std::unary_function<T*, void> {
public:
  void operator()(T* t) {
    t->print<S>();
  };
};

int main() {
  op<int, Foo> p;

  p(new Foo());
}
The error g++ gives me is:

Code:
> g++ -o test test.cpp
test.cpp: In member function 'void op<S, T>::operator()(T*)':
test.cpp:18: error: expected primary-expression before '>' token
test.cpp:18: error: expected primary-expression before ')' token
g++ is version 4.1.2 20061115 on a x86_64 debian 4.0. If I change the line

Code:
    t->print<S>();
to

Code:
    t->Foo::print<S>();
the program compiles, but I can't use this... The problem looks like a name resolution problem. I tried to google, but haven't found anything so far. Any suggestions?

Thanks,

matze
 
Old 04-09-2008, 11:10 AM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Why not t->T::print<S>();? I don't see anything fundamental that would keep the name from being resolved, but it is an odd set of circumstances that isn't often used.
ta0kira

Last edited by ta0kira; 04-09-2008 at 11:13 AM.
 
Old 04-09-2008, 11:21 AM   #3
matze
Member
 
Registered: Oct 2003
Posts: 50

Original Poster
Rep: Reputation: 15
Thanks, but I tried that and it does not work - it gives an identical error message

(The background is that I want to build a generic index where the key is to be defined in the template (just like S is used here) and is retrieved by a method that the used classes (T) share...)

matze
 
Old 04-09-2008, 11:58 AM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Maybe you should use an abstract class instead of a template in Foo? Being an index, there has to be something in common with all of them.
ta0kira
 
Old 04-09-2008, 12:07 PM   #5
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
The function is dependant on a template parameter, prefix with template
Code:
  void operator()(T* t) {
    t->template print<S>();
  };
 
Old 04-10-2008, 09:26 AM   #6
matze
Member
 
Registered: Oct 2003
Posts: 50

Original Poster
Rep: Reputation: 15
@ta0kira: I considered that (or specializing the template), as my classes share a parent class, but that would do it only as a temporary workaround.

@dmail: Thanks, that solved it.

Thanks to both of you for your answers.

matze
 
  


Reply



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++ templated Node class: pointers to different instantated class types jhwilliams Programming 3 08-20-2007 06:20 PM
How to register deeper member function in above class? kornerr Programming 3 01-30-2007 02:27 PM
passing a class member function to pthread_create. dmail Programming 1 07-29-2006 11:15 AM
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

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

All times are GMT -5. The time now is 12:03 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