cb: unreferenced local variable

rickhill11
rickhill11 used Ask the Experts™
on
I have a class, call if foo.
namespace foo6
{
   class foo
   {
   static int Trim(char *buffer);
   ...
   }
}


In the cpp file

...OnInitialUpdate()
{
    Cview::On...
   foo6::foo fb;

   A couple of lines later
   fb.Trim(buff);
}

Everything seems to work exactly as advertised, except that I get the unreferenced local variable message from the compiler.

What am I doing wrong?

Rick
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Karrtik IyerSoftware Architect

Commented:
Where is buff (input to trim) declared and defined in your code?
Also Trim being a static function of Foo, you do not need an object Foo to invoke it.
You can use it with class name and scope resolution, like below
foo::Trim(buffer).
Karrtik IyerSoftware Architect

Commented:
Can you please share your Complete code of cpp file and point the line where error is coming from compiler? We can help you better if you share this information.
brgdotnetcontractor

Commented:
Karrtik is correct. Please if you could describe where buff is declare, and defined. Also is it assigned a value? Share that code, and we can better help you.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

The message probably says what is the identifier of the unreferenced variable.
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
That is no bug necessarily. You just have defined a local variable, but do not use it (assign a value, use it as parameter,...).

Author

Commented:
Karrtik,  I'm playing around with static functions within a class, and I'm very aware of the ability to do the namespace::class::Trim()l, but I wanted to instantiate the class, and then make the call class.Trim() to see and understand any different behavior.

To All,

The full warning is: mfcapplication3view.cpp(143): warning C4101: 'cb' : unreferenced local variable

The minimal code that will generate this error is:

in the .h file

#include <stdexcept>

#include <ctype.h>

namespace Cbase6
{
      class CbaseStrings
      {
      public:
            static int Trim(char *buffer);
        }
}

In the .cpp file

#include "CbaseStrings.h"

using namespace std;

namespace Cbase6
{

            int CbaseStrings::Trim(char *str,char ToBeTrimmed)
            {
                  int x;
                  if(!(x=strlen(str)))return 0;
                  for(--x;ToBeTrimmed?(*(str+x)==ToBeTrimmed):(isspace(*(str+x)))&&x>0;x--);
                  if(ToBeTrimmed?(*(str+x)==ToBeTrimmed):(isspace(*(str+x)))){
                        *(str+x)=0;
                        return 0;
                  }
                  else *(str+x+1)=0;
                  return x+1;
            }
            int CbaseStrings::Trim(char *str)
            {
                  return Trim(str,0);
            }
       }
}

in the .cpp file that actually calls this dll/library

void CMFCApplication3View::OnInitialUpdate()
{
      CView::OnInitialUpdate();

      // TODO: Add your specialized code here and/or call the base class
      char Buff[35]="hello out there   1111";
      int ii;
      Cbase6::CbaseStrings cb;
      ii=cb.Trim(Buff,'1');
}

So, unless I'm on crack today, Cbase6::CbaseStrings cb instantiates an instance of the class CbaseStrings using the namespace Cbase6 and that instance is referenced by cb.

cb.Trim() actually uses that instance of the class, and this is what is happening when I compile and trace the code using the debugger.  It goes right into Trim, and executes that section of code.

This is not a major issue, but I simply am averse to having compiler/linker warnings hanging around for not apparent reason.  When I get one, I want to do whatever is reasonable deal with it.

Thanks in advance.
Software Architect
Commented:
Hi Rick, this warning is generated because Trim is a static function and compiler identifies that the cb variable is of no use and is not used to call this function and other than calling Trim nothing else is done using the cb variable. Hence it says cb is not referenced (used) in the warning.
So try adding a public non static member function in your class. And just to understand this behaviour call that non static function using cb. And compile your code, the warning shall go away. Refer the link below for more details.
https://googleweblight.com/?lite_url=https://msdn.microsoft.com/en-us/library/c733d5h9.aspx&ei=Bx7rgrkv&lc=en-IN&s=1&m=497&ts=1450270056&sig=ALL1Aj6CXVb_x82XRe0iqLWnMuWW8fiXGA
Karrtik IyerSoftware Architect

Commented:
To understand this even further, when you debug your Trim static function, try typing "?this" in the immediate window (Debug Menu -> Windows -> Immediate Window), it shall say " 'this' may only be used inside a nonstatic member function".
Since this pointer (which is nothing but the cb variable) is of no use, the compiler warns that this is something which is not referenced and hence not needed.
Karrtik IyerSoftware Architect

Commented:
If you want to further understand memory layout and different data segments in a typical C++ program which uses GCC, you can refer the link below, I found it useful. This describes data is stored in what segments.
http://www.gahcep.com/cpp-internals-memory-layout/

Author

Commented:
Thanks for the very complete answer.

Rick
Karrtik IyerSoftware Architect

Commented:
You are welcome Rick.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial