Link to home
Start Free TrialLog in
Avatar of deleyd
deleydFlag for United States of America

asked on

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

Avatar of sarabande
sarabande
Flag of Luxembourg image

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
Avatar of deleyd

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of phoffric
phoffric

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of HooKooDooKu
HooKooDooKu

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).
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