Solved

Can private data member be assigned to a global variable ?

Posted on 2013-12-09
12
330 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
 
LVL 1

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

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
 
LVL 1

Author Comment

by:naseeam
ID: 39707466
created SetValue public method of same class.  Same compile error.
0
 
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
 
LVL 1

Author Comment

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

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
 
LVL 1

Author Comment

by:naseeam
ID: 39708528
OK.  Thanks!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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.

688 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