cb: unreferenced local variable

I have a class, call if foo.
namespace foo6
   class foo
   static int Trim(char *buffer);

In the cpp file

   foo6::foo fb;

   A couple of lines later

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

What am I doing wrong?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Karrtik IyerSoftware ArchitectCommented:
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
Karrtik IyerSoftware ArchitectCommented:
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.
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.
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

The message probably says what is the identifier of the unreferenced variable.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
That is no bug necessarily. You just have defined a local variable, but do not use it (assign a value, use it as parameter,...).
rickhill11Author 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
            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;
                        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()

      // TODO: Add your specialized code here and/or call the base class
      char Buff[35]="hello out there   1111";
      int ii;
      Cbase6::CbaseStrings cb;

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.
Karrtik IyerSoftware ArchitectCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Karrtik IyerSoftware ArchitectCommented:
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 ArchitectCommented:
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.
rickhill11Author Commented:
Thanks for the very complete answer.

Karrtik IyerSoftware ArchitectCommented:
You are welcome Rick.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.