Jan 22, 2013 at 11:57am Jan 22, 2013 at 11:57am UTC
Hi all,
I have this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <cstdarg>
template < typename T >
void print( int numArgs, ... ) {
va_list vl;
va_start( vl, numArgs );
for ( int i = 0; i < numArgs; i++ ) {
T arg = va_arg( vl, T );
std::cout << arg;
}
}
int main( int argc, char * argv[] ) {
print( 2, "Hello, " , " World!" );
return 0;
}
When I compile it, I get the following error: error: no matching function for call to 'print(int, const char [8], const char [8])'
What is wrong with the code?
Thanks!
Last edited on Jan 22, 2013 at 11:58am Jan 22, 2013 at 11:58am UTC
Jan 22, 2013 at 12:11pm Jan 22, 2013 at 12:11pm UTC
you need to provide the template parameter an line 16 because the compiler can't deduce it from the parameter
Jan 22, 2013 at 5:31pm Jan 22, 2013 at 5:31pm UTC
@coder777: so, if I'm right, the compiler needs to know the types of ...
at compile time?
@JLBorges: your code doesn't compile, generates alot of errors because of the dots. Do I have to replace the three dots with something?
Jan 22, 2013 at 6:07pm Jan 22, 2013 at 6:07pm UTC
OK, right, I tried with Codepad :) Thank you!
Jan 24, 2013 at 4:06pm Jan 24, 2013 at 4:06pm UTC
[bump] But still the question: so, if I'm right, the compiler needs to know the types of ...
at compile time? Is that true?
Jan 24, 2013 at 4:46pm Jan 24, 2013 at 4:46pm UTC
Yes. But with the first example you could simply...
print<const char *>( 2, "Hello, " , " World!" );
Last edited on Jan 24, 2013 at 4:46pm Jan 24, 2013 at 4:46pm UTC