[LC++]How to define a static map member?

Shaul Karl shaulka at bezeqint.net
Tue Apr 2 17:06:05 UTC 2002


I am trying to have get a static member of a class to be of type 
    map<const string, pointer*>.

What is the correct way for doing it?

/*
 * main.cc.
 */


#include <algorithm>
#include <iostream>
#include <map>
#include <stdexcept>
#include <string>

using namespace std;


class category
{
    public:
        category(const string&)  throw(runtime_error);
    private:
        static map<const string, category*> allCategories;
};


category::category(const string &name)
throw(runtime_error)
try
{
    allCategories.insert(map<const string, category*>::value_type(
                                                           name, this));
}
catch(const exception &excp) { throw; }


int main(void)
try {
    return 0;
}
catch(const exception &excp) {
    cerr << excp.what() << "\n";
    throw;
}


$ g++-3.0 -Wall main.cc
/tmp/ccOmjj03.o: In function `category::category[not-in-charge](std::bas
ic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/tmp/ccOmjj03.o(.text+0x29): undefined reference to 
`category::allCategories'
/tmp/ccOmjj03.o: In function `category::category[in-charge](std::basic_s
tring<char, std::char_traits<char>, std::allocator<char> > const&)':
/tmp/ccOmjj03.o(.text+0x109): undefined reference to 
`category::allCategories'
collect2: ld returned 1 exit status
$


When I have tried to add a

    map<const string, category*> catergory::allCategories;

outside of the class I got a syntax error:

main.cc:23: syntax error before `::' token

-- 

    Shaul Karl
    email: shaulka(at-no-spam)bezeqint.net 
           Please substitute (at-no-spam) with an at - @ - character.
           (at-no-spam) is meant for unsolicitate mail senders only.






More information about the tuxCPProgramming mailing list