Solved

Can private data member be assigned to a global variable ?

Posted on 2013-12-09
12
318 Views
Last Modified: 2013-12-10
/* 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
0
Comment
Question by:naseeam
  • 6
  • 4
  • 2
12 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 100 total points
ID: 39707422
Technically, this is allowed. However, the nature of your error messages suggests that there is something else going on, can you post a bit more of your code?
0
 

Author Comment

by:naseeam
ID: 39707430
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

0
 
LVL 86

Expert Comment

by:jkr
ID: 39707439
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...
0
 

Author Comment

by:naseeam
ID: 39707448
I just tried it.  I get the same compile error.  I'm using Keil tools.  I wonder if this is a compiler bug.
0
 
LVL 86

Expert Comment

by:jkr
ID: 39707454
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?
0
 

Author Comment

by:naseeam
ID: 39707466
created SetValue public method of same class.  Same compile error.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 32

Accepted Solution

by:
phoffric earned 400 total points
ID: 39707477
jkr's solution may work if it avoids possible name collision. I noticed that you have a number of tokens named "available", the global one and:
      unsigned short available() { return _avail;};

Then you do this:
     available = _avail;

It could be that you are trying to set the function name, available, to a value rather than the global? I would rename the function to:
     unsigned short getAvailable() { return _avail;};
0
 

Author Comment

by:naseeam
ID: 39707490
That worked!   Great Catch!
0
 

Author Closing Comment

by:naseeam
ID: 39707492
Thank you!  Changing function name compile successfully.   Genius find!
0
 
LVL 86

Expert Comment

by:jkr
ID: 39707625
Good find, Paul! ;o)
0
 
LVL 32

Expert Comment

by:phoffric
ID: 39707741
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.
0
 

Author Comment

by:naseeam
ID: 39708528
OK.  Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now