473,327 Members | 1,896 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.

Template member functions in template class with separate definition

The following code works:

#include <iostream>

template<class T>
struct Test
{
T t;
template<class T, template<class U = Tclass V>
Test<T>& foo(V<T>& f)
{
f.t = 5;
return *this;
}
};

int main() {
Test<intt;
t.foo(t);
std::cout << t.t;

}

However, if I try to move the definition of the foo()-function outside
of the declaration like so:

template<typename T, template<typename U = Tclass V>
Test<T>& Test<T>::foo(V<T>& f)
{
f.t = 5;
return *this;
}

It does not compile (VC++8.0) with the following error-message:

test.cpp(24) : error C2244: 'Test<T>::foo' : unable to match function
definition to an existing declaration
definition
'Test<T&Test<T>::foo(V<T&)'
existing declarations
'Test<T&Test<T>::foo(V<T&)'

Anyone have an idea of how to make this work?

--
Erik Wikström

Dec 14 '06 #1
5 2345
Erik Wikström wrote:
The following code works:

#include <iostream>

template<class T>
struct Test
{
T t;
template<class T, template<class U = Tclass V>
Test<T>& foo(V<T>& f)
{
f.t = 5;
return *this;
}
};

int main() {
Test<intt;
t.foo(t);
std::cout << t.t;

}

However, if I try to move the definition of the foo()-function outside
of the declaration like so:

template<typename T, template<typename U = Tclass V>
Test<T>& Test<T>::foo(V<T>& f)
{
f.t = 5;
return *this;
}

It does not compile (VC++8.0) with the following error-message:

test.cpp(24) : error C2244: 'Test<T>::foo' : unable to match function
definition to an existing declaration
definition
'Test<T&Test<T>::foo(V<T&)'
existing declarations
'Test<T&Test<T>::foo(V<T&)'

Anyone have an idea of how to make this work?
The default argument in the function _definition_ looks wrong to me.

--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
Dec 14 '06 #2
On Dec 14, 1:03 pm, "Steven T. Hatton" <chatten...@germania.supwrote:
Erik Wikström wrote:
The following code works:
#include <iostream>
template<class T>
struct Test
{
T t;
template<class T, template<class U = Tclass V>
Test<T>& foo(V<T>& f)
{
f.t = 5;
return *this;
}
};
int main() {
Test<intt;
t.foo(t);
std::cout << t.t;
}
However, if I try to move the definition of the foo()-function outside
of the declaration like so:
template<typename T, template<typename U = Tclass V>
Test<T>& Test<T>::foo(V<T>& f)
{
f.t = 5;
return *this;
}
It does not compile (VC++8.0) with the following error-message:
test.cpp(24) : error C2244: 'Test<T>::foo' : unable to match function
definition to an existing declaration
definition
'Test<T&Test<T>::foo(V<T&)'
existing declarations
'Test<T&Test<T>::foo(V<T&)'
Anyone have an idea of how to make this work?

The default argument in the function _definition_ looks wrong to me.
You mean the U = T part? I tried with "template<class T,
template<classclass V>" instead but with no luck.

--
Erik Wikström

Dec 14 '06 #3
Erik Wikström wrote:
#include <iostream>

template<class T>
struct Test
{
T t;
template<class T, template<class U = Tclass V>
Test<T>& foo(V<T>& f)
{
f.t = 5;
return *this;
}
};

int main() {
Test<intt;
t.foo(t);
std::cout << t.t;

}

However, if I try to move the definition of the foo()-function outside
of the declaration like so:

template<typename T, template<typename U = Tclass V>
Test<T>& Test<T>::foo(V<T>& f)
{
f.t = 5;
return *this;
}
The first form doesn't compile for me with GCC 4.0.2. Have I got it right?

//templ.cpp
#include <iostream>

template<class T>
struct Test
{
T t;
template<class T, template<class U = Tclass V>
Test<T>& foo(V<T>& f)
{
f.t = 5;
return *this;
}
};

int main() {
Test<intt;
t.foo(t);
std::cout << t.t;
}
//---------------EOF-----------------

g++ -o templ templ.cpp
templ.cpp:7: error: declaration of ?class T?
templ.cpp:3: error: shadows template parm ?class T?

Compilation exited abnormally with code 1 at Thu Dec 14 07:47:26

--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
Dec 14 '06 #4

Erik Wikström wrote:
The following code works:

#include <iostream>

template<class T>
struct Test
{
T t;
template<class T, template<class U = Tclass V>
Test<T>& foo(V<T>& f)
{
f.t = 5;
return *this;
}
};

int main() {
Test<intt;
t.foo(t);
std::cout << t.t;

}
The program is using "T" as the name of two separate type parameters,
So I would suggest giving each parameter a distinct name, maybe T1 and
T2, so that the compiler can tell them part.

Greg

Dec 14 '06 #5
On Dec 14, 1:52 pm, "Steven T. Hatton" <chatten...@germania.supwrote:
>
The first form doesn't compile for me with GCC 4.0.2. Have I got it right?

//templ.cpp
#include <iostream>

template<class T>
struct Test
{
T t;
template<class T, template<class U = Tclass V>
Test<T>& foo(V<T>& f)
{
f.t = 5;
return *this;
}

};int main() {
Test<intt;
t.foo(t);
std::cout << t.t;}//---------------EOF-----------------

g++ -o templ templ.cpp
templ.cpp:7: error: declaration of ?class T?
templ.cpp:3: error: shadows template parm ?class T?
Yes, that looks right, however I finally managed to find a working
solution:

#include <iostream>

template<class T>
struct Test
{
T t;
template<template<class U = Tclass V>
Test<T>& foo(V<T>& f);
};

template<class T>
template<template<classclass V>
Test<T>& Test<T>::foo(V<T>& f)
{
f.t = 5;
return *this;
}

int main() {
Test<intt;
t.foo(t);
std::cout << t.t;

}

Once again, the "template<template<class U = Tclass V>" can be
reduced to "template<template<classclass V>" but I think that it
documents the intention that the type that V is parametrized by is the
same as that of Test.

--
Erik Wikström

Dec 14 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem...
4
by: Martin MacRobert | last post by:
Hi Gang, The following code does not compile, but I can't figure out why. The compiler complains that the CuriouslyDerivedType (CRDerived) does not publish the "value_type", yet in fact the class...
7
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
5
by: Ruben Campos | last post by:
Some questions about this code: template <typename T> class MyTemplate; template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object); template <typename T> MyTemplate <T>...
3
by: sks | last post by:
Hello all Is the usage of extern keyword valid for telling the compiler to NOT instantiate a template and to link it from an another binary? For example: Suppose module A's binary contains a...
2
by: coolpint | last post by:
Can anyone kindly provide an explanation as to why the compiler does not "see" the function template in the contrieved code below? I think the argument deduction fails but can't figure out...
8
by: nishit.gupta | last post by:
I was having a problem with template class memer function definition , so i serched the net and find that template class member fuction definition should be in header file else compilation will...
9
by: stephen.diverdi | last post by:
Can anyone lend a hand on getting this particular template specialization working? I've been trying to compile with g++ 4.1 and VS 2005. ...
6
by: Gaijinco | last post by:
I'm trying to do a template class Node. My node.hpp is: #ifndef _NODE_HPP_ #define _NODE_HPP_ namespace com { namespace mnya { namespace carlos { template <typename T>
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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
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.