Why does Visual Studio say "unreferenced local variable"?

When I compile Visual Studio says "Warning C4101: 'm' : unreferenced local variable".

But I'm using 'm' on the very next line after defining it.

#include "stdafx.h"
#include "machine.h"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
  Machine m;
  m.operate();
  return 0;
}

Open in new window


Here's the rest of my test code: Machine.h
#include "stdafx.h"
#include <iostream>
using namespace std;

class Machine
{
public:
	static void operate();
};	

Open in new window

Machine.cpp
#include "stdafx.h"
#include "Machine.h"

void Machine::operate() {
  cout << "Hello world" <<endl;
}

Open in new window

deleydSoftware EngineerAsked:
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.

sarabandeCommented:
it is for the arguments argc and argv which are not (yet) used.

to get rid of the warning you could do

int _tmain(int /* argc */, _TCHAR* /* argv */ [] )
{
     ...

Open in new window


or set a pragma statement above your code:

#pragma warning (disable: 4101)

Open in new window


note, the latter will suppress warning C4101 also for local variables defined but not used.

Sara
1
deleydSoftware EngineerAuthor Commented:
That doesn't seem to be it. I changed the code to:
#pragma warning (disable: 4101)
#include "stdafx.h"
#include "machine.h"
using namespace std;

int _tmain()
{
  Machine m;
  m.operate();
  return 0;
}

Open in new window

still get the 'm' not used warning.
0
phoffricCommented:
>>       static void operate();

Remove the static, or leave the static and change:

  Machine m;
  m.operate();

to:
  Machine::operate();

Leaving the static in means that using m. syntax is really a so-called convenience for Machine::operate(). So, the compiler probably is transforming the m. into a Machine:: and so m is not being used. Your compiler is set to be very strict.
0

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
HooKooDooKuCommented:
Just to add to phoffic's comments:

When you add the 'static' keyword to the declaration of a class member variable, what you are basically doing is creating a global variable with class scope.  You don't have to create and instance of the class for the member variable to be created... and if the member variable is public, you don't even have to have an instance of the class to access it.  If you do create instances of the class, all instances will access the one copy of the global variable.

But that's for member variables.  When you create a static member function, you are creating a class function that can only access static member variables.  And just like with the static member variable, if the static member function is declared public (as is your case) then you don't even have to have an instance of the class to call the member function, you simply need scope (hence the reason you can access the member function with Machine::operate() without creating an instance of the class).
0
sarabandeCommented:
That doesn't seem to be it. I changed the code to:

#pragma warning (disable: 4101)
#include "stdafx.h"

still get the 'm' not used warning.


because of precompiled headers, all statements (also preprocessor statements) above #include stdafx.h were NOT processed (but ignored) by the compiler.

you would need to move the pragma below the include statement, but as you now have a good solution, there is no need for the pragma.

Sara
0
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
C++

From novice to tech pro — start learning today.