Avatar of naseeam
naseeam
Flag for United States of America asked on

Can private data member be assigned to a global variable ?

/* global variable */
unsigned short available;

/* private data member of class SocketBuffer */
volatile unsigned short _avail;

/* private member function of class SocketBuffer */
unsigned short  read  ( unsigned char *dest, unsigned short length )
{
     ...
     available = _avail;    /* Is this allowed ?  */
     ....
}


 Attached is compile error.
compile-error.PNG
C++

Avatar of undefined
Last Comment
naseeam

8/22/2022 - Mon
SOLUTION
jkr

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
naseeam

ASKER
unsigned short available;   /* global variable */

class SocketBuffer
{
  public:

    /// <summary>Constructor
    /// </summary>
    SocketBuffer();
    SocketBuffer(unsigned short Buffsize); 
    /// <summary>Descructor
    /// </summary>
    ~SocketBuffer();    
    bool           isFull()    { return (_size == _avail);};
    unsigned short available() { return _avail;};
    unsigned short freespace() { return _size - _avail;};
    unsigned short size()      { return _size;};
    unsigned short dump(unsigned short size);
    unsigned short getchunksize() { return _size-_out;};
    virtual  void  reset();
    unsigned short read     ( unsigned char *dest, unsigned short length )
    { // make sure there is room in buffer
      unsigned short cnt = 0;
      if (os_mut_wait(&_mutex, 0xffff) != OS_R_TMO)
      {

        available = _avail;    

        while ((cnt < length) && (_avail))
        {
//					dbg_print("SocketBuffer::read : cnt = %d, _buf[cnt] = %X\n", cnt, _buf[cnt]);
          dest[cnt++] = _buf[_out++];
          if (_out == _size)
            _out = 0;
          _avail--;
					
        }  
        os_mut_release(&_mutex);
      }
//			tot_bytes_read += cnt;
//			dbg_print("SocketBuffer::read : total bytes read = %d  \n", tot_bytes_read);
      return cnt;
    };
    
    unsigned short write    ( unsigned char *src,  unsigned short length )
    { // make sure there is room in buffer
      unsigned short cnt = 0;
      if (os_mut_wait(&_mutex, 0xffff) != OS_R_TMO)
      {
        if (_size - _avail < length)
          length = _size - _avail;
        while (cnt < length)
        {
          _buf[_in++] = src[cnt++];
          if (_in == _size)
            _in = 0;
          _avail++;
        }  
        os_mut_release(&_mutex);
      }
      return cnt;
    };


    unsigned short isr_write    ( unsigned char *src,  unsigned short length )
    { // make sure there is room in buffer
      unsigned short cnt = 0;
			
			
      if (_size - _avail < length)
        length = _size - _avail;
      while (cnt < length)
      {
        _buf[_in++] = src[cnt++];
        if (_in == _size)
          _in = 0;
        _avail++;
				
      }
			
// debug code
			tot_bytes_in_sw_buf += length;
//			if (tot_bytes_in_sw_buf >= 796796)
////				 tot_bytes_in_sw_buf = 0;
//      }				
//      dbg_print("SocketBuffer::isr_write : total bytes in sw buffer = %ld \n", tot_bytes_in_sw_buf);	
			
      return cnt;
    };
    
  private:
    OS_MUT         _mutex;
    unsigned char *_buf;
    unsigned short _size;
    volatile unsigned short _in;
    volatile unsigned short _out;
    volatile unsigned short _avail;

};  // end of class SocketBuffer

Open in new window

jkr

Hm, again, that *should* work - have you tried giving the global variable exactly the same qualifiers? E.g.

/* global variable */
volatile unsigned short available;

Open in new window


even though that does not really make sense for this one...
naseeam

ASKER
I just tried it.  I get the same compile error.  I'm using Keil tools.  I wonder if this is a compiler bug.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
jkr

Could well be possible, especially given the first error message. One shot into the dark - does this

unsigned short available;   /* global variable */

void SetValue(unsigned short us) {

  available = us;
}

// ...

    unsigned short read     ( unsigned char *dest, unsigned short length )
    { 

      // ...

     SetValue(_avail);

    // ...

    } 

Open in new window


help?
naseeam

ASKER
created SetValue public method of same class.  Same compile error.
ASKER CERTIFIED SOLUTION
phoffric

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
naseeam

ASKER
That worked!   Great Catch!
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
naseeam

ASKER
Thank you!  Changing function name compile successfully.   Genius find!
jkr

Good find, Paul! ;o)
phoffric

You're welcome. BTW, you might consider adopting a naming convention for global variables; e.g., possibly prefix all of them with "g_". This is useful in a multi-threading environment where that prefix serves as a flag that a mutex may be needed if the global is shared across threads.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
naseeam

ASKER
OK.  Thanks!