null - Can't return nullptr to my class C++ -
in method in class, i'm checking if value 0 return nullptr
, can't seem that.
complex complex::sqrt(const complex& cmplx) { if(cmplx._imag == 0) return nullptr; return complex(); }
the error i'm getting is: could not convert 'nullptr' 'std::nullptr_t' 'complex'
i realize now, nullptr
pointers, however, object not pointer, there way me set null or similar?
you returning complex
, not pointer. in order return nullptr
, return type should complex*
.
noticed edit - here's can do:
bool complex::sqrt(const complex& cmplx, complex& out) { if(cmplx._imag == 0) { // out won't set here! return false; } out = complex(...); // set out parameter here return true; }
call this:
complex resultofsqrt; if(sqrt(..., resultofsqrt)) { // resultofsqrt guaranteed set here } else { // resultofsqrt wasn't set }
Comments
Post a Comment