Forward template declaration problem

Discussion in 'C++' started by Jure Erznožnik, Mar 12, 2011.

  1. I'd like to hide implementation details from declaration because
    dragging all the support types from .cpp to .h would be unnecessary,
    redundant and just plain ugly.
    So I try to forward-declare the classes used in actual implementation.
    This works well for ordinary classes, but not so well for templates
    (std::multimap in this case).

    I know the typedef from the following sample redeclares my desired
    type, but I included the code because I don't know any better. I also
    included a sample for MyClass class which compiles fine - for
    reference of what I want to do.

    So how can I make the below code work without:
    1. Dragging all the necessary types into .h
    2. Declaring jobList member as void * and then typecasting all over
    the place
    3. Using #define to mask the typecasting from #2

    Please forgive the fact that I'm still learning C++.

    Thanks,
    Jure

    //sample.h here
    class JobListMap;
    class MyClass;

    class RefClock
    {
    public:
    RefClock();
    JobListMap *jobList;
    MyClass *test;
    };

    //sample.cpp from now on
    #include "sample.h"
    #include <map>

    struct MyType1 {
    ....
    };
    struct MyType2 {
    ....
    };
    class MyClass {
    };

    typedef std::multimap<MyType1, MyType2> JobListMap;

    RefClock::RefClock()
    {
    jobList = new JobListMap();
    test = new MyClass();
    }
    Jure Erznožnik, Mar 12, 2011
    #1
    1. Advertising

  2. Jure Erznožnik

    Bo Persson Guest

    Jure Erznoznik wrote:
    > I'd like to hide implementation details from declaration because
    > dragging all the support types from .cpp to .h would be unnecessary,
    > redundant and just plain ugly.
    > So I try to forward-declare the classes used in actual
    > implementation. This works well for ordinary classes, but not so
    > well for templates (std::multimap in this case).
    >
    > I know the typedef from the following sample redeclares my desired
    > type, but I included the code because I don't know any better. I
    > also included a sample for MyClass class which compiles fine - for
    > reference of what I want to do.
    >
    > So how can I make the below code work without:
    > 1. Dragging all the necessary types into .h
    > 2. Declaring jobList member as void * and then typecasting all over
    > the place
    > 3. Using #define to mask the typecasting from #2
    >
    > Please forgive the fact that I'm still learning C++.
    >
    > Thanks,
    > Jure
    >
    > //sample.h here
    > class JobListMap;
    > class MyClass;
    >
    > class RefClock
    > {
    > public:
    > RefClock();
    > JobListMap *jobList;
    > MyClass *test;
    > };
    >
    > //sample.cpp from now on
    > #include "sample.h"
    > #include <map>
    >
    > struct MyType1 {
    > ...
    > };
    > struct MyType2 {
    > ...
    > };
    > class MyClass {
    > };
    >
    > typedef std::multimap<MyType1, MyType2> JobListMap;


    No way! :)

    Earlier you said that JobListMap is a class, now you say it's a
    typedef. Can't do that!

    A possible, but ugly, way of actually making it a class is

    class JobListMap : public std::multimap<MyType1, MyType2>
    {
    // whatever constructors you need here
    };


    >
    > RefClock::RefClock()
    > {
    > jobList = new JobListMap();
    > test = new MyClass();
    > }



    Bo Persson
    Bo Persson, Mar 12, 2011
    #2
    1. Advertising

  3. Tried declaring that, but the compiler doesn't like me:

    class JobListMap: public std::multimap<MyType1, MyType2>
    {
    public:
    JobListMap(): std::multimap<MyType1, MyType2>() {}
    };

    I get the folowing errors:
    refclock.cpp(89) : error C2512: 'bitlib::JobListMap' : no appropriate
    default constructor available
    refclock.cpp(100) : warning C4150: deletion of pointer to incomplete
    type 'bitlib::JobListMap'; no destructor called
    include\blaf/refclock.h(33) : see declaration of
    'bitlib::JobListMap'
    refclock.cpp(108) : error C2027: use of undefined type
    'bitlib::JobListMap'
    include\blaf/refclock.h(33) : see declaration of
    'bitlib::JobListMap'
    refclock.cpp(108) : error C2227: left of '->insert' must point to
    class/struct/union/generic type

    So, is there a non-ugly way of doing this?
    Jure Erznožnik, Mar 12, 2011
    #3
  4. On 3/12/2011 6:15 AM, Jure Erznožnik wrote:
    > Tried declaring that, but the compiler doesn't like me:
    >
    > class JobListMap: public std::multimap<MyType1, MyType2>
    > {
    > public:
    > JobListMap(): std::multimap<MyType1, MyType2>() {}
    > };
    >
    > I get the folowing errors:
    > refclock.cpp(89) : error C2512: 'bitlib::JobListMap' : no appropriate
    > default constructor available


    Did you forget to include the proper header where your 'JobListMap'
    class is defined? This error is likely to occur when the compiler can
    only see the forward declaration.

    > refclock.cpp(100) : warning C4150: deletion of pointer to incomplete
    > type 'bitlib::JobListMap'; no destructor called
    > include\blaf/refclock.h(33) : see declaration of
    > 'bitlib::JobListMap'
    > refclock.cpp(108) : error C2027: use of undefined type
    > 'bitlib::JobListMap'
    > include\blaf/refclock.h(33) : see declaration of
    > 'bitlib::JobListMap'
    > refclock.cpp(108) : error C2227: left of '->insert' must point to
    > class/struct/union/generic type
    >
    > So, is there a non-ugly way of doing this?


    You haven't done enough yet to claim ugliness. Get rid of the compile
    errors first, make it work. Then you can see whether it's nice or ugly.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Mar 12, 2011
    #4
  5. On Mar 12, 3:31 pm, Victor Bazarov <> wrote:
    > On 3/12/2011 6:15 AM, Jure Erzno nik wrote:
    >
    > > Tried declaring that, but the compiler doesn't like me:

    >
    > > class JobListMap: public std::multimap<MyType1, MyType2>
    > > {
    > > public:
    > >    JobListMap(): std::multimap<MyType1, MyType2>() {}
    > > };

    >
    > > I get the folowing errors:
    > > refclock.cpp(89) : error C2512: 'bitlib::JobListMap' : no appropriate
    > > default constructor available

    >
    > Did you forget to include the proper header where your 'JobListMap'
    > class is defined?  This error is likely to occur when the compiler can
    > only see the forward declaration.
    >
    > > refclock.cpp(100) : warning C4150: deletion of pointer to incomplete
    > > type 'bitlib::JobListMap'; no destructor called
    > >          include\blaf/refclock.h(33) : see declaration of
    > > 'bitlib::JobListMap'
    > > refclock.cpp(108) : error C2027: use of undefined type
    > > 'bitlib::JobListMap'
    > >          include\blaf/refclock.h(33) : see declaration of
    > > 'bitlib::JobListMap'
    > > refclock.cpp(108) : error C2227: left of '->insert' must point to
    > > class/struct/union/generic type

    >
    > > So, is there a non-ugly way of doing this?

    >
    > You haven't done enough yet to claim ugliness.  Get rid of the compile
    > errors first, make it work.  Then you can see whether it's nice or ugly..
    >
    > V
    > --
    > I do not respond to top-posted replies, please don't ask


    Turns out I messed up with namespaces.
    My header was in my_named_space.
    In the source file I had
    using my_name_space;

    but the
    class MyType ......

    was in default namespace :(

    fixed with class my_name_space::MyType

    ....

    Thanks for all the help,
    Jure
    Jure Erznožnik, Mar 12, 2011
    #5
    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. jobseeker
    Replies:
    2
    Views:
    1,090
    DarkSpy
    Oct 15, 2003
  2. Nobody
    Replies:
    2
    Views:
    49,008
    Nobody
    Feb 12, 2004
  3. qazmlp
    Replies:
    1
    Views:
    593
    Jonathan Turkanis
    Feb 15, 2004
  4. verec
    Replies:
    2
    Views:
    1,032
    verec
    Jun 25, 2005
  5. steprobe
    Replies:
    3
    Views:
    488
    Vladimir Jovic
    Apr 7, 2010
Loading...

Share This Page