Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Confusing C2065 'undeclared identifier' error
Forum Updated to NodeBB v4.3 + New Features

Confusing C2065 'undeclared identifier' error

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 2.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nevdokimof
    wrote on last edited by nevdokimof
    #1

    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 it

    I believe it has smth to do with declaration in if statement because if I declare frontArrayoutside of it it works just fine. In my case it's ok to declare frontArray there, if audioDatais empty function just returns and don't do anything with undeclared variable.
    Declaring frontArray also in else 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

    1 Reply Last reply
    0
    • mzimmersM Offline
      mzimmersM Offline
      mzimmers
      wrote on last edited by mzimmers
      #2

      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) 
          {
      ...
      
      1 Reply Last reply
      3
      • N Offline
        N Offline
        nevdokimof
        wrote on last edited by
        #3

        Yep, frontArray just goes out of scope. Thanks.

        Cpp reference says:

        if (x)
            int i;
        // i is no longer in scope
        
        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved