1

I am coding in c++ and I am attempting to learn about static variables.
When I wrote my practice code, I got this error message:

Undefined symbols for architecture x86_64:
"pizza::firstLetterFavPizza", referenced from:
pizza::favPizzaFirstLetterChan(char) in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

Please help! I don't know what's wrong. The source code is here:

#include <iostream>
class pizza
{
 public: 
    static char firstLetterFavPizza;

    char favPizzaFirstLetterChan (char letter = firstLetterFavPizza)
    {
     pizza::firstLetterFavPizza = letter;
     return pizza::firstLetterFavPizza;
    }
};

int main()
{
    pizza *a = new pizza();
    pizza *b = new pizza();
    std::cout << a->favPizzaFirstLetterChan('c') << std::endl;
    delete a;
    std::cout << b->favPizzaFirstLetterChan('b') << std::endl;
    delete b;
    return 0;
};
Cache Staheli
  • 3,510
  • 7
  • 32
  • 51

1 Answers1

1

You have declared static data member, but not defined it. Add a definition to your code somewhere (in the global namespace):

char pizza::firstLetterFavPizza;
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084