Calling device function from host function error

I am getting this error “calling a device function from a host function is not allowed” but I am not sure what’s wrong, here is the code:

class dlbool {

    int     dvalue;

__device__    explicit dlbool(int v) : dvalue(v) { }


__device__    dlbool()       : dvalue(0) { }

__device__    dlbool(bool x) : dvalue((int)x*2-1) { }

__device__    int dtoInt(void) const { return dvalue; }

__device__    bool  operator == (const dlbool& dother) const { return dvalue == dother.dvalue; }

__device__    bool  operator != (const dlbool& dother) const { return dvalue != dother.dvalue; }

__device__    dlbool operator ~  (void)               const { return dlbool(-dvalue); }

__device__    friend int   dtoInt  (dlbool l);

__device__    friend dlbool dtoLbool(int   v);


__device__ inline int   dtoInt  (dlbool l) { return l.dtoInt(); }

__device__ inline dlbool dtoLbool(int   v) { return dlbool(v);  }

const dlbool dl_True  = dtoLbool( 1);

const dlbool dl_False = dtoLbool(-1);

const dlbool dl_Undef = dtoLbool( 0);

The error I am getting (actually 3 errors) are from the last 3 lines. I know that “dtoLbool” is the one causing the error but I don’t see where the host here since everything is declared device. The code is part of a .cuh file. Your help is much appreciated…

Your constant variables in the last 3 lines are declared on the host, thus the host is trying to initialize them. However, the initialization is using device functions. Hence the error.

Thanks for the reply. However, I still don’t get it. I tried putting device before the last lines but it still gives the same error. How can I declare it in the device?

A call there is being run on the host, but you’re calling a device function.

Try declaring your functions device host instead of just device.