1

Basically, I want to call a static function in my class which adds an entry to a static member of type std::map.

class Foo
{
    private:
        static std::map<std::string, int, StringCompare> mymap;
    public:
        static bool addEntry(std::string id);
};

std::map<std::string, int, StringCompare> Foo::mymap;

static bool Foo::addEntry(std::string id)
{
    int a = 0;
    return (mymap.insert ( std::pair<std::string, int> (id, a))).second;
}

EDIT: Forgot to ask the question D:

When I compile this code, it gives me the error:

derp.hpp:24:41: error: cannot declare member function ‘static bool Foo::addEntry(std::string)’ to have static linkage [-fpermissive]

What do I do?

StAlRuth
  • 59
  • 6
  • If all of the class members are going to be static, why not get rid of the class and use a namespace?, if you want to keep some functions private, just doesn't publish it on the header file. – PaperBirdMaster Oct 04 '12 at 08:22
  • How do you mean "just doesn't publish it on the header file"? – StAlRuth Oct 04 '12 at 08:26
  • IMHO create a class with all members static is pointless and isn't a class at all because none of the members belongs to any instance of the class. So, i think that is better to change the class with a namespace, creating both `cpp` and `h` files and publish the methods you want to be public on the header file, keeping the methods that you want to be private on the `cpp` file. – PaperBirdMaster Oct 04 '12 at 08:30

1 Answers1

2

For your original "question", use this:

Foo::addEntry("myId");

For your forgotten question, simply remove the static keyword.

Mark Garcia
  • 17,424
  • 4
  • 58
  • 94