By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,323 Members | 1,291 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,323 IT Pros & Developers. It's quick & easy.

Templated member function help

P: 3
I'm having some problems with a templated member function of a templated class. Unfortunately I can't replicate it with a simple example so I know something odd must be going on!!!

Basically it's like this...

I have a few classes all templated by one type. Inside one of the classes is a templated member function. When I try to use this templated function in another class, the compiler complains with:

error: expected primary-expression before 'int'
error: expected ',' or ';' before 'int'

The odd thing is that if I remove the <int> from the call to the function it compiles fine. Currently this part of the code isn't actually used as I never create an instance of the class, it's just there in my code for later. So I am thinking this might be something to do with it.

It's confusing I know and I'll try to get a simple example working.

I think basically the compiler isn't noticing that the function I am calling is a template and so it's wondering why I am putting in the <int>. How can I force it to know it's going to be a template?
Mar 15 '07 #1
Share this Question
Share on Google+
2 Replies

P: 3
Ok here's an example:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  4. template <typename T>
  5. class One {
  6.     public:
  7.         One() {;}
  8.         ~One() {;}
  10.         template <typename TT>
  11.         TT doSomething(TT var1);
  12.     private:
  13. };
  15. template <typename T>
  16. template <typename TT>
  17. TT One<T>::doSomething(TT var1) {
  18.     return var1;
  19. }
  21. template <typename T>
  22. class Two {
  23.     public:
  24.         Two(One<T> *a);
  25.         ~Two() {;}
  26.     private:
  27.         One<T> *_one;
  28. };
  30. template <typename T>
  31. Two<T>::Two(One<T> *a) {
  32.     _one = a;
  33.     int ret = _one->doSomething<int>(5);
  34. }
  36. int main() {
  37.     return 0;
  38. }
The output from the compiler is:
Expand|Select|Wrap|Line Numbers
  1. memfunc.cpp: In constructor 'Two<T>::Two(One<T>*)':
  2. memfunc.cpp:33: error: expected primary-expression before 'int'
  3. memfunc.cpp:33: error: expected ',' or ';' before 'int'
It's annoying... basically inside the compiler for Two, it's not noticing that doSomething is a template and so it complains about the <int> part of that line...

What am I doing wrong?
Mar 15 '07 #2

P: 3
Ok, WHAT! If I remove the <int> it works, compiles fine. And then if I change it slightly to actually use the template (I made the doSomething function save to a public int inside class Two and then cout'ed that value to check it saved it).

It seemed to work out the <int> itself. This is really wierd because it shouldn't do that, should it!?

Like for instance if I did:

Expand|Select|Wrap|Line Numbers
  1. template <typename T>
  2. T doSomething(T var1) {
  3.    return var1;
  4. }
(i.e. a templated function, not member function)
Then it'd be WRONG to do:
Expand|Select|Wrap|Line Numbers
  1. int ret = doSomething(5)
I would have to do:
Expand|Select|Wrap|Line Numbers
  1. int ret = doSomething<int>(5)
I am mega confused...
Mar 15 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.