-1

I am working on ns2.35 but i want to access a variable from a program connector.cc #include "packet.h" #include "connector.h"

int attacker = 0;

static class ConnectorClass : public TclClass {

to aodv.cc

#include <connector.h>
extern int attacker;

then

if (malicious == 1000){
  printf("\nDROPD\n");
drop(p, DROP_RTR_ROUTE_LOOP);
}

printf("\nAttacker: %d\n", &attacker);

For example i declared a variable int attacker = 0; in connector.cc and i want to access this variable in aodv.cc How can i do that? I am not able to do so using the above code. I am using c++.

Thank you in advance :)

1 Answers1

0

In C++, variable linkage is internal by default.

This means that you can't get access to a variable in a different translation unit by declaring it extern where you want to use it.
If you want to access the variable, it must have been declared extern in the translation unit that defines it.

The most common way of accomplishing this is to add the extern declaration (but not any definition) to a header and include that both where the variable is used and where it is defined.

(And your printf is wrong - you shouldn't use the address-of operator when printing an int.)

molbdnilo
  • 64,751
  • 3
  • 43
  • 82