Template problem

R

Richard Hayden

Hi,

I'm just learning about C++ templates and can't understand why the
following code is not working as expected:

/*************************************\

#include <iostream>

template<class C>C func(C c) {
return c;
}

int main(int argc, char** argv) {
std::cout << afunc(23); // OUTPUTS '5' INSTEAD OF '23'
}

/*************************************\

I'd be very grateful if someone could explain to me why this code is not
working as expected and outputting '5' instead of '23'.

Thanks,
 
A

Ali Cehreli

Hi,

I'm just learning about C++ templates and can't understand why the
following code is not working as expected:

/*************************************\

#include <iostream>

template<class C>C func(C c) {
return c;
}

int main(int argc, char** argv) {
std::cout << afunc(23); // OUTPUTS '5' INSTEAD OF '23'

This is not 'func' but 'afunc'.
}

/*************************************\

I'd be very grateful if someone could explain to me why this code is not
working as expected and outputting '5' instead of '23'.

Thanks,

In the part of your code that you did not post, there is a function called
afunc that outputs 5.

Ali
 
R

Russell Hanneken

Richard said:
I'm just learning about C++ templates and can't understand why the
following code is not working as expected:

/*************************************\

#include <iostream>

template<class C>C func(C c) {
return c;
}

int main(int argc, char** argv) {
std::cout << afunc(23); // OUTPUTS '5' INSTEAD OF '23'
}

/*************************************\

I'd be very grateful if someone could explain to me why this code is not
working as expected and outputting '5' instead of '23'.

It's hard to say without seeing the definition of afunc. Or did you
mean to call func rather than afunc?
 

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
474,334
Messages
2,571,755
Members
48,554
Latest member
CarrollCro

Latest Threads

Top