C4189, why?
-
I have cleaned and rebuilt this project a couple of times just to see if this still comes up, I am getting:
C4189: 'pTruth': local variable is initialised but not referenced
The function this is occurring on is:
void truth::setEnabled(bool blnEnable) { if ( truth::mspSpinMax != nullptr ) { truth::mspSpinMax->setEnabled(blnEnable); } if ( truth::mspSpinMin != nullptr ) { truth::mspSpinMin->setEnabled(blnEnable); } if ( truth::mspSlider != nullptr ) { truth::mspSlider->setEnabled(blnEnable); truth::mspSlider->setVisible(blnEnable); } foreach( truth* pTruth, truth::mslstAllTruths ) { pTruth->setEnabled(false); } }
The yellow warning icon appears on the foreach line, clearly pTruth is used in the loop, so why is this warning being displayed?
setEnabled public static function, here is the prototype:
static void setEnabled(bool blnEnable);
I am using Qt 5.9.2 with MSVC2015 on Windows 10.
-
@SPlatten said in C4189, why?:
foreach
also, please use the standard range for loop instead of foreach/Q_FOREACH there are reasons, why its deprecated and I believe even removed from Qt6 :D
-
Because setEnabled() is a static function - no need for pTruth.
-
@SPlatten said in C4189, why?:
The yellow warning icon appears on the foreach line, clearly pTruth is used in the loop, so why is this warning being displayed?
setEnabled public static function, here is the prototype:
static void setEnabled(bool blnEnable);Why do you use a foreach loop to got access to a static method?
You don't need class instances to access static methods. I guess this is why you get this warning... But I am not a C++ expert, I may be wrong. -
@Christian-Ehrlicher , that doesn't make sense because in the function it is iterating through a list and clearly using the pointer.
-
@KroMignon, @Christian-Ehrlicher , setEnabled is not just a static function in my truth class, it is also a Qt function that isn't static and is part of the QWidget class which truth is derived from.
-
@SPlatten said in C4189, why?:
foreach
also, please use the standard range for loop instead of foreach/Q_FOREACH there are reasons, why its deprecated and I believe even removed from Qt6 :D
-
@SPlatten said in C4189, why?:
setEnabled is not just a static function in my truth class, it is also a Qt function that isn't static and is part of the QWidget class which truth is derived from.
This is not what you told us in your first post. How should we know this?
-
@J-Hilk said in C4189, why?:
@SPlatten said in C4189, why?:
foreach
also, please use the standard range for loop instead of foreach/Q_FOREACH there are reasons, why its deprecated and I believe even removed from Qt6 :D
It has been un-deprecated and is still present in Qt6.
But your recommendation is still very valid :-) It is better to use ranged for, with
qAsConst()
. -
I have another issue which having resolved the original problem by using for( : ), I've now found that although:
pTruth->setEnabled(blnEnable);
Is intended to call the QWidget version of setEnabled, it is actually calling the static version even though the correct way to call the static version would be:
truth::setEnabled(blnEnable);
Is there a way I can resolve this?
-
@sierdzio said in C4189, why?:
It has been un-deprecated and is still present in Qt6.
oO ok, I should follow the mailing list more closely...
personally nqAsConst() and such have always bothered me,
since c++20 we can now give the for loop init arguments so something like this should be possible :DQVector<int> v; for( auto const cVector = v; auto entry : cVector){ }
-
@SPlatten said in C4189, why?:
Is there a way I can resolve this?
call explicitly the base class method:
pTruth->QWidget::setEnabled(blnEnable)
-
@SPlatten said in C4189, why?:
Is there a way I can resolve this?
Do not create a static function with the same name as the inherited one.
However, I do not really understand why all of the UI elements (mspSpinMax etc.) and this function are static in the first place.
-
@SPlatten said in C4189, why?:
have another issue which having resolved the original problem by using for( : ), I've now found that although:
pTruth->setEnabled(blnEnable);Is intended to call the QWidget version of setEnabled, it is actually calling the static version even though the correct way to call the static version would be:
truth::setEnabled(blnEnable);Is there a way I can resolve this?
AFAIK, you should never have static and class methods with same signature in C++ classes, because the last one will hide the previous one.
But again, I am not a C++ expert, maybe there is a way to do it which I don't know.