1

I've been trying to solve this problem for hours and hours... I have a header file, implementation file and a driver file. HEADER:

class PhoneNumber
{
 private:

  const int MAXTEXTS;
  static int live;
  static int text; // number of total texts from all the phones.                                                                     
  string areaCode;
  string exchange;
  string line;
  int nlive;
  int ntext; // number of texts sent on this phone                                                                                   
public:

  static int MaxPhones;
  PhoneNumber();
  PhoneNumber(string, string, string, int);
  void inputPhoneNumber();
  void displayPhoneNumber();
  void sendText();
  void dialNum();
  int getLive();
  int getText();
  int getnLive();
  int getnText();
  static void addLive()
  {
    live++;
  }
  static void addText()
  {
    text++;
  }

};

IMPLEMENTATION:

int PhoneNumber::getnLive()
{
  return nlive;
}

int PhoneNumber::getnText()
{
  return ntext;
}

int PhoneNumber::getLive()
{
  return live;
}

int PhoneNumber::getText()
{
  return text;
}

error message:

habins-mbp:CS2000 Habin$ g++ -o PhoneNumber PhoneNumber.cpp PhoneNumberDriver.cpp

Undefined symbols for architecture x86_64:
  "PhoneNumber::live", referenced from:
      PhoneNumber::getLive() in PhoneNumber-f64d4d.o
      PhoneNumber::addLive() in PhoneNumber-f64d4d.o
  "PhoneNumber::text", referenced from:
      PhoneNumber::getText() in PhoneNumber-f64d4d.o
      PhoneNumber::addText() in PhoneNumber-f64d4d.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

IF I use g++ -c, it compiles meaning that the code works. It seems like the static int live is giving me so much trouble. Been trying to solve this for 10+ hours and to no avail. I'm about to snap my computer in half!

please help me

PersianGulf
  • 2,845
  • 6
  • 47
  • 67
habs
  • 15
  • 4

1 Answers1

1

You should define all the static member variables in IMPLEMENTATION part.

int PhoneNumber::live;
int PhoneNumber::text;
int PhoneNumber::MaxPhones;
timrau
  • 22,578
  • 4
  • 51
  • 64
  • hmm. so you define the variables in the IMPLEMENTATION part, and functions-and-their-definitions in the HEADER part? – habs Mar 22 '14 at 02:43
  • [This](http://stackoverflow.com/questions/9282354/static-variable-link-error) is already answered here. – DigitalEye Mar 22 '14 at 02:46
  • define? How do you define a variable? I know how to declare a variable. Very new to programming, thank you for your help! – habs Mar 22 '14 at 02:48
  • GOT IT TO WORK. I just had to add those lines. I can't believe I spent so much time in this stupid error but thank you everybody. – habs Mar 22 '14 at 02:52