Confusing C2065 'undeclared identifier' error
-
I have this code:
void AudioBuffer::write(QByteArray *byteArray) { if (!audioData.isEmpty()) auto frontArray = audioData.front(); else { audioData.enqueue(*byteArray); return; } if (frontArray.size() == sizeOfOneArray) // C2065: 'frontArray': undeclared identifier { audioData.enqueue(*byteArray); return; } if ( (frontArray.size() + byteArray->size()) <= sizeOfOneArray) // C2065: 'frontArray': undeclared identifier { frontArray.append(*byteArray); // C2065: 'frontArray': undeclared identifier return; } else { int32_t remainSize = sizeOfOneArray - frontArray.size(); // C2065: 'frontArray': undeclared identifier for(int i = 0; i < remainSize; ++i) { //some work } } }
frontArray
has been declared but QtCreator somehow isn't sure about itI believe it has smth to do with declaration in
if
statement because if I declarefrontArray
outside of it it works just fine. In my case it's ok to declarefrontArray
there, ifaudioData
is empty function just returns and don't do anything with undeclared variable.
DeclaringfrontArray
also inelse
statement doesn't change anything.Header file:
#include <QObject> #include <QQueue> class AudioBuffer : public QObject { Q_OBJECT private: QQueue<QByteArray> audioData; int32_t sizeOfOneArray = 50 * 32; public: explicit AudioBuffer(QObject *parent = nullptr); AudioBuffer (int32_t _sizeOfOneArray, QObject *parent = nullptr); QByteArray read(); void write(QByteArray* byteArray); };
Qt Creator 4.7.0, compiler is MSVC 2015
-
I believe your analysis is correct -- the problem is that the variable is defined within an invisible unit that consists only of the declaration of the variable itself. In other words, it's gone immediately after it's defined.
There are several fixes; the simplest one (though not necessarily the best example of coding) is this:
void AudioBuffer::write(QByteArray *byteArray) { if (audioData.isEmpty()) audioData.enqueue(*byteArray); return; } auto frontArray = audioData.front(); if (frontArray.size() == sizeOfOneArray) { ...
-
Yep,
frontArray
just goes out of scope. Thanks.Cpp reference says:
if (x) int i; // i is no longer in scope