views:

63

answers:

3
+1  Q: 

Cannot cout data

Hi, Guys, reffering to last post I'm trying to output data while template is instantiated

template <unsigned long N>
   struct binary
   {
       std::cout << N;//<---------------------------------I'M TRYING HERE
       static unsigned const value
          = binary<N/10>::value << 1   // prepend higher bits
            | N%10;                    // to lowest bit

but I'm getting an error:

'Error 2 error C2886: 'std::cout' : symbol cannot be used in a member using- declaration '

Thanks for help P.S. And could anyone explain why actually I can't do that?

+4  A: 

I'm trying to output data while template is instantiated

Template instantiation happens at compile-time. You can't output anything at compile-time.

All you can do is calculate the value at compile-time and the output it at run-time (i.e. inside a function).

sepp2k
+2  A: 

You're inside a struct not inside a method. Only there can you call functions.

Till Theis
+2  A: 

You can declare variables, and assign them values there. But something like that needs to be inside a function.

Matt Blaine