class factory linking issue

Discussion in 'C++' started by Aaron Prillaman, Jul 23, 2004.

  1. I'm trying to set up a class factory for loading classes from a
    database. Each table will be loaded into it's own class with a common
    base. Just thought I'd try a simple sample first, but I'm having
    trouble getting it to link. Here's the code and output:

    class c1{
    public:
    virtual void fn()=0;
    };

    class factory{
    private:
    static std::map<int,c1*(*)()> load_fns;
    public:
    factory(int key,c1*(*pfn)()){
    load_fns.insert(std::make_pair(key,pfn));
    }
    static c1* Make(int key){
    return load_fns[key]();
    }
    };

    class c2 : public c1{
    public:
    void fn(){ std::cout << "running c2 fn\n";}
    static c1* ld(){return new c2;}
    static factory f;
    };
    factory c2::f(2,&c2::ld);

    class c3 : public c1{
    public:
    void fn(){ std::cout << "running c3 fn\n";}
    static c1* ld(){return new c3;}
    static factory f;
    };
    factory c3::f(3,&c3::ld);


    int main(int argc, char *argv[]){

    c1* pc = factory::Make(2);
    c1* pc2 = factory::Make(3);

    pc->fn();
    pc2->fn();

    delete pc;
    delete pc2;

    return 0;
    }

    main.obj : error LNK2001: unresolved external symbol "private: static
    class std::map<int,class c1 * (__cdecl*)(void),struct
    std::less<int>,class std::allocator<class c1 * (__cdecl*)(void)> >
    factory::load_fns" (?load_fns@factory@@0V?$map@HP6APAVc1@@
    XZU?$less@H@std@@V?$allocator@P6APAVc1@@XZ@3@@std@@A)
    Release/test.exe : fatal error LNK1120: 1 unresolved externals
     
    Aaron Prillaman, Jul 23, 2004
    #1
    1. Advertising

  2. Aaron Prillaman

    Sharad Kala Guest

    "Aaron Prillaman" <> wrote in message
    news:0o6Mc.31248$SD3.15597@okepread06...
    > I'm trying to set up a class factory for loading classes from a
    > database. Each table will be loaded into it's own class with a common
    > base. Just thought I'd try a simple sample first, but I'm having
    > trouble getting it to link. Here's the code and output:
    >
    > class c1{
    > public:
    > virtual void fn()=0;
    > };
    >
    > class factory{
    > private:
    > static std::map<int,c1*(*)()> load_fns;
    > public:
    > factory(int key,c1*(*pfn)()){
    > load_fns.insert(std::make_pair(key,pfn));
    > }
    > static c1* Make(int key){
    > return load_fns[key]();
    > }
    > };


    Add this line -
    std::map<int,c1*(*)()> factory::load_fns;

    -Sharad
     
    Sharad Kala, Jul 23, 2004
    #2
    1. Advertising

  3. "Sharad Kala" <> wrote in message
    news:...
    >
    > "Aaron Prillaman" <> wrote in message
    > news:0o6Mc.31248$SD3.15597@okepread06...
    > > I'm trying to set up a class factory for loading classes from a
    > > database. Each table will be loaded into it's own class with a common
    > > base. Just thought I'd try a simple sample first, but I'm having
    > > trouble getting it to link. Here's the code and output:
    > >
    > > class c1{
    > > public:
    > > virtual void fn()=0;
    > > };
    > >
    > > class factory{
    > > private:
    > > static std::map<int,c1*(*)()> load_fns;
    > > public:
    > > factory(int key,c1*(*pfn)()){
    > > load_fns.insert(std::make_pair(key,pfn));
    > > }
    > > static c1* Make(int key){
    > > return load_fns[key]();
    > > }
    > > };

    >
    > Add this line -
    > std::map<int,c1*(*)()> factory::load_fns;
    >


    In a source file, not a header file.

    john
     
    John Harrison, Jul 23, 2004
    #3
  4. John Harrison wrote:
    > "Sharad Kala" <> wrote in message
    > news:...
    >
    >>"Aaron Prillaman" <> wrote in message
    >>news:0o6Mc.31248$SD3.15597@okepread06...
    >>
    >>>I'm trying to set up a class factory for loading classes from a
    >>>database. Each table will be loaded into it's own class with a common
    >>>base. Just thought I'd try a simple sample first, but I'm having
    >>>trouble getting it to link. Here's the code and output:
    >>>
    >>>class c1{
    >>>public:
    >>> virtual void fn()=0;
    >>>};
    >>>
    >>>class factory{
    >>>private:
    >>> static std::map<int,c1*(*)()> load_fns;
    >>>public:
    >>> factory(int key,c1*(*pfn)()){
    >>> load_fns.insert(std::make_pair(key,pfn));
    >>> }
    >>> static c1* Make(int key){
    >>> return load_fns[key]();
    >>> }
    >>>};

    >>
    >>Add this line -
    >>std::map<int,c1*(*)()> factory::load_fns;
    >>

    >
    >
    > In a source file, not a header file.
    >
    > john
    >
    >

    doh!
    sorry that should have been obvious..
    guess i was just up too late working on this

    thanks a lot for the help,
    Aaron
     
    Aaron Prillaman, Jul 23, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Medi Montaseri
    Replies:
    17
    Views:
    919
    Medi Montaseri
    Sep 3, 2003
  2. Simon Elliott
    Replies:
    0
    Views:
    397
    Simon Elliott
    Jan 11, 2005
  3. Richard

    Abstract class linking issue

    Richard, Oct 11, 2006, in forum: C++
    Replies:
    4
    Views:
    355
    Richard
    Oct 12, 2006
  4. C#
    Replies:
    4
    Views:
    439
  5. Alan
    Replies:
    4
    Views:
    270
    Bill Felton
    Jan 27, 2011
Loading...

Share This Page