Solved

Can private data member be assigned to a global variable ?

Posted on 2013-12-09
12
313 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

758 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

21 Experts available now in Live!

Get 1:1 Help Now