Templated function inside a template class. Syntax.

T

Thormod Johansen

Hi,

What is the correct syntax for defining a templated function inside a
template class? Example:

template <class T>
class myClass {
private:
T data;
public:
template <class InputType>
int myFunc(InputType arg) {
}
};

To this my compiler says "declaration terminated incorrectly" at the line
containing "template <class InputType>".

Thanks in advance.
 
V

Victor Bazarov

Thormod said:
What is the correct syntax for defining a templated function inside a
template class? Example:

template <class T>
class myClass {
private:
T data;
public:
template <class InputType>
int myFunc(InputType arg) {

I'd venture a guess that you need a 'return' statement here.
}
};

To this my compiler says "declaration terminated incorrectly" at the
line containing "template <class InputType>".

I don't see a problem in the code you posted. Could you post the
_exact_ *complete* program that causes the error?

V
 
V

Victor Bazarov

Thormod said:
Here is an excat complete example for which my compiler (paradigm c++)
generates the same error the same place.

#include <embedded.h>

template <class T>
class myClass {
private:
T data;
public:
myClass(T t) : data(t) {};

template <class InputType>
int myFunc(InputType arg) {
outport(0x500, arg);
return 0;
}
};

void main() {
myClass<unsigned char> c(2);

int value = 0xDC;
c.myFunc(value);

while(1)
;
}

Thanks in advance

That's not a complete example. <embedded.h> is missing...

Never mind that. Drop the include, drop the call to 'outport',
replace 'void main' with 'int main', and you get a correct C++
program. It should compile with any compliant compiler. Now,
if it doesn't compile with the compiler you're using, it is
apparently not compliant and cannot handle member templates (it
is not unheard of). Complain to people from whom you got it.

V
 
T

Thormod Johansen

Victor Bazarov said:
I don't see a problem in the code you posted. Could you post the
_exact_ *complete* program that causes the error?

Here is an excat complete example for which my compiler (paradigm c++)
generates the same error the same place.

#include <embedded.h>

template <class T>
class myClass {
private:
T data;
public:
myClass(T t) : data(t) {};

template <class InputType>
int myFunc(InputType arg) {
outport(0x500, arg);
return 0;
}
};

void main() {
myClass<unsigned char> c(2);

int value = 0xDC;
c.myFunc(value);

while(1)
;
}

Thanks in advance
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top