0

I'm working on a school assignment. I have Wallet.h and Main.cpp In the Wallet.h I have a public:

static std::map<std::string, double> currencies;

and Wallet();

In Main.cpp I'm trying to use this currencies map as such:

std::map<std::string, double> currencies = wallet.currencies;`

I do not get any warnings in the code, but when I try to compile I get this error:

Undefined symbols for architecture x86_64: "Wallet::currencies", [...] ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

I'm not sure what is wrong, probably it's not correct how I'm trying to use currencies. Can someone help?

  • 1
    Please **[edit]** your question with an [mre] or [SSCCE (Short, Self Contained, Correct Example)](http://sscce.org) – NathanOliver Dec 28 '20 at 14:19
  • Most likely your question is a duplicate of this: https://stackoverflow.com/questions/9110487/undefined-reference-to-a-static-member – NathanOliver Dec 28 '20 at 14:20
  • 1
    step 1: realize that "undefined symbol" errors are not _compiler_ but _linker_ errors... – Dietmar Kühl Dec 28 '20 at 14:23
  • Take inspiration from existing, working, open source code on https://github.com/ or http://gitlab.com/, e.g. [Clang static analyzer](https://clang-analyzer.llvm.org/) or [fish](https://fishshell.com/) or [RefPerSys](http://refpersys.org/) – Basile Starynkevitch Dec 28 '20 at 15:17

2 Answers2

2

Hard to say anything specific without a proper minimal reproducible example, but it seems that with

std::map<std::string, double> currencies = wallet.currencies;

you define a global non-member variable instead of a Wallet member.

You need to explicitly define it as a member of the Wallet class:

std::map<std::string, double> Wallet::currencies = wallet.currencies;
//                            ^^^^^^^^
//    Note scope as Wallet member here
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Well, the linker error contains sufficient information to know that it is a member of class and the definition provided isn't. Of course, the original question should still include all relevant bits which it didn't. – Dietmar Kühl Dec 28 '20 at 14:25
  • Thanks for the answers. I tried std::map Wallet::currencies, it's not working. I'm not sure how to share the relevant bits without sharing the whole project. It's quite big and not public. –  Dec 28 '20 at 14:34
0

Ok I figured, I needed to add std::map<std::string, double> Wallet::currencies = {{"USD", 10000}}; to Wallet.cpp to default the values. Wallet was empty.

This question pointed me in the direction of finding the problem: c++ error: :-1: error: symbol(s) not found for architecture x86_64 - in Qt-Creator