Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Can private data member be assigned to a global variable ?

Posted on 2013-12-09
12
Medium Priority
?
337 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 400 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
Technology Partners: 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 1600 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

609 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