0

I have a class named CallMe that have a static method accessing a static variable...

// callme.h

class CallMe{

    static std::map<std::string,int> myMap;

public:

    // ... Other functions for adding removing etc...

    static int Get(std::string name)
    {
        int value = 0;
        // finding value ...
        return value;
    }
};

//  callme.cpp
std::map<std::string,int> CallMe::myMap

And i am using CallMe class from other class that resides in a header file user.h

// user.h

#include "callme.h"

class User{

public:

    int Call(std::string name)
    {
        return CallMe::Get(name);
    }
}; 

All the code are in header files(except CallMe::myMap declaration) When I compile app. I get "undefined CallMe".

is this error happening because I defined the classes in the header files or cause of using static variable...

aphelix
  • 29
  • 1
  • 4
  • 1
    Please don't paraphrase an error, copy and paste into the question. – Yksisarvinen Mar 04 '20 at 08:53
  • 1
    The first problem I see with your code is a missing semicolon after `std::map CallMe::myMap` - but not sure (yet) if that's the *only* problem. – Adrian Mole Mar 04 '20 at 08:53
  • 1
    My crystal ball says you forgot to include the compiled callme.cpp when you are linking the final binary together. – Botje Mar 04 '20 at 08:55
  • Forgot to include the compiled callme.cpp ? Is it possible to include cpp files? – aphelix Mar 04 '20 at 09:04
  • Apparently I am able to close questions without votes now. I wanted the question "does this answer your question?" to pop up when I voted to close. Oh well, if OP pastes the full error message into the question and it doesn't look like a linking error, I'll reopen. – Ted Lyngmo Mar 04 '20 at 09:07
  • @user3379281 Botje also wrote "_when you are linking the final binary together_". How many `.cpp` files have you created for this project? How are you compiling them? How are you linking? What is the full error message? – Ted Lyngmo Mar 04 '20 at 09:30
  • 1
    @TedLyngmo You recently earned the Gold Badge for the `C++` tag! Congratulations - you now have "Thor's Hammer" for closing questions as duplicates. – Adrian Mole Mar 04 '20 at 09:37
  • @AdrianMole :-) I'll wield Mjölnir more carefully in the future. – Ted Lyngmo Mar 04 '20 at 09:50

0 Answers0