473,327 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Templated member function help

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
2 1667
Ok here's an example:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template <typename T>
  5. class One {
  6.     public:
  7.         One() {;}
  8.         ~One() {;}
  9.  
  10.         template <typename TT>
  11.         TT doSomething(TT var1);
  12.     private:
  13. };
  14.  
  15. template <typename T>
  16. template <typename TT>
  17. TT One<T>::doSomething(TT var1) {
  18.     return var1;
  19. }
  20.  
  21. template <typename T>
  22. class Two {
  23.     public:
  24.         Two(One<T> *a);
  25.         ~Two() {;}
  26.     private:
  27.         One<T> *_one;
  28. };
  29.  
  30. template <typename T>
  31. Two<T>::Two(One<T> *a) {
  32.     _one = a;
  33.     int ret = _one->doSomething<int>(5);
  34. }
  35.  
  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
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

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

Similar topics

1
by: Rich | last post by:
Hi, I have a query regarding VC6 and its handling of templated copy constructors. Here goes: Take a look at the following code sample... template<class _Ty, size_t t_uiSize = 10 > class...
3
by: William Payne | last post by:
Consider this (templated) class member function: template<typename Type> void CircularContainer<Type>::insert(const Type& s) { vector<Type>::iterator itr = find(m_elements.begin(),...
9
by: Jon Wilson | last post by:
I have a class which needs to accumulate data. The way we get this data is by calling a member function which returns float on a number of different objects of different type (they are all the...
4
by: Lionel B | last post by:
Greetings, The following code: <code> template<typename T> class A { protected:
1
by: Trevor Morgan | last post by:
I've been having problems when a templated member function has the same name as ANY previously defined template parameter. Here's the simplest case that demonstrates the problem. This fails to...
6
by: Dan Huantes | last post by:
I was presented a problem today where a class had member variable that was an object of a templated class. The class wanted to instantiate the object as a private member variable and call a...
11
by: Matthias | last post by:
Hello, templated virtual member functions are not allowed. Now I am searching for a good workaround for this problem, but I can't find any. Here's my problem: class Base { template...
15
by: kwikius | last post by:
Hi all, I have been searching through the list of papers at: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/ I seem to remember a proposal regarding templated namespaces e.g: template...
2
by: Amadeus W. M. | last post by:
I have a bunch of templated functions: template <class Type_t> double f2(Type_t x) { return 2*x; } template <class Type_t> double f3(Type_t x) { return 3*x; }
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.