Namespace/Template member initialization issue

Discussion in 'C++' started by Brian Ross, Aug 17, 2003.

  1. Brian Ross

    Brian Ross Guest

    Hi,

    I am having a problem writing a constructor/member initialization with
    VC.NET7.1.

    Here is the code:

    ---

    namespace Library
    {
    template <typename T>
    class LibraryTemplateT
    { };
    }

    namespace Module
    {
    class ModuleObject : public Library::LibraryTemplateT<int>
    {
    public:
    ModuleObject() throw();
    };

    // [1] FAILS: error C2614: 'Module::ModuleObject' : illegal member
    initialization: 'LibraryTemplateT' is not a base or member
    // ModuleObject::ModuleObject() throw() : LibraryTemplateT()
    // { }

    // [2] FAILS: error C2059: syntax error : '<'
    // ModuleObject::ModuleObject() throw() : LibraryTemplateT<int>()
    // { }

    // [3] WORKS
    ModuleObject::ModuleObject() throw() : Library::LibraryTemplateT<int>()
    { }

    }
    ---

    The weird thing is that if LibraryTemplateT is a class and not a template
    then I am able to use the first option above and not have to manually
    specify the Library namespace. Also, all three choices work when trying them
    online at http://www.comeaucomputing.com/tryitout/.

    Since I have existing code that is using option [1] above for non-templates
    I would like to know:

    - Is the code illegal (for both templates and regular classes). ie. is
    option [3] the only legal way to write that code (without having a using
    declaration)?
    - Are templates somehow special and get resolved differently?
    - Is this a bug?

    Thanks
    Brian Ross, Aug 17, 2003
    #1
    1. Advertising

  2. "Brian Ross" <> wrote in message
    news:ZHL%a.212759$...
    > Hi,
    >
    > I am having a problem writing a constructor/member initialization with
    > VC.NET7.1.
    >
    > Here is the code:
    >
    > ---
    >
    > namespace Library
    > {
    > template <typename T>
    > class LibraryTemplateT
    > { };
    > }
    >
    > namespace Module
    > {
    > class ModuleObject : public Library::LibraryTemplateT<int>
    > {
    > public:
    > ModuleObject() throw();
    > };
    >
    > // [1] FAILS: error C2614: 'Module::ModuleObject' : illegal member
    > initialization: 'LibraryTemplateT' is not a base or member
    > // ModuleObject::ModuleObject() throw() : LibraryTemplateT()
    > // { }
    >
    > // [2] FAILS: error C2059: syntax error : '<'
    > // ModuleObject::ModuleObject() throw() : LibraryTemplateT<int>()
    > // { }
    >
    > // [3] WORKS
    > ModuleObject::ModuleObject() throw() :

    Library::LibraryTemplateT<int>()
    > { }
    >
    > }
    > ---
    >
    > The weird thing is that if LibraryTemplateT is a class and not a template
    > then I am able to use the first option above and not have to manually
    > specify the Library namespace. Also, all three choices work when trying

    them
    > online at http://www.comeaucomputing.com/tryitout/.
    >
    > Since I have existing code that is using option [1] above for

    non-templates
    > I would like to know:
    >
    > - Is the code illegal (for both templates and regular classes). ie. is
    > option [3] the only legal way to write that code (without having a using
    > declaration)?
    > - Are templates somehow special and get resolved differently?
    > - Is this a bug?
    >
    > Thanks
    >


    The following code compiles for me using VC++ 7.1

    namespace Library
    {
    template <typename T>
    class LibraryTemplateT
    { };
    }
    namespace Module
    {
    class ModuleObject : public Library::LibraryTemplateT<int>
    {
    public:
    ModuleObject() throw() {}
    };
    }
    int main()
    {
    Module::ModuleObject m;
    }

    Suggest you post the exact code you are having trouble with.

    john
    John Harrison, Aug 17, 2003
    #2
    1. Advertising

  3. Brian Ross

    Brian Ross Guest

    "John Harrison" <> wrote in message
    news:bho2j5$1cqfp$-berlin.de...
    >
    > "Brian Ross" <> wrote in message
    > news:ZHL%a.212759$...
    > > Hi,
    > >
    > > I am having a problem writing a constructor/member initialization with
    > > VC.NET7.1.
    > >
    > > Here is the code:
    > >
    > > ---
    > >
    > > namespace Library
    > > {
    > > template <typename T>
    > > class LibraryTemplateT
    > > { };
    > > }
    > >
    > > namespace Module
    > > {
    > > class ModuleObject : public Library::LibraryTemplateT<int>
    > > {
    > > public:
    > > ModuleObject() throw();
    > > };
    > >
    > > // [1] FAILS: error C2614: 'Module::ModuleObject' : illegal member
    > > initialization: 'LibraryTemplateT' is not a base or member
    > > // ModuleObject::ModuleObject() throw() : LibraryTemplateT()
    > > // { }
    > >
    > > // [2] FAILS: error C2059: syntax error : '<'
    > > // ModuleObject::ModuleObject() throw() : LibraryTemplateT<int>()
    > > // { }
    > >
    > > // [3] WORKS
    > > ModuleObject::ModuleObject() throw() :

    > Library::LibraryTemplateT<int>()
    > > { }
    > >
    > > }
    > > ---
    > >
    > > The weird thing is that if LibraryTemplateT is a class and not a

    template
    > > then I am able to use the first option above and not have to manually
    > > specify the Library namespace. Also, all three choices work when trying

    > them
    > > online at http://www.comeaucomputing.com/tryitout/.
    > >
    > > Since I have existing code that is using option [1] above for

    > non-templates
    > > I would like to know:
    > >
    > > - Is the code illegal (for both templates and regular classes). ie. is
    > > option [3] the only legal way to write that code (without having a using
    > > declaration)?
    > > - Are templates somehow special and get resolved differently?
    > > - Is this a bug?
    > >
    > > Thanks
    > >

    >
    > The following code compiles for me using VC++ 7.1
    >
    > namespace Library
    > {
    > template <typename T>
    > class LibraryTemplateT
    > { };
    > }
    > namespace Module
    > {
    > class ModuleObject : public Library::LibraryTemplateT<int>
    > {
    > public:
    > ModuleObject() throw() {}
    > };
    > }
    > int main()
    > {
    > Module::ModuleObject m;
    > }
    >
    > Suggest you post the exact code you are having trouble with.


    Hi,

    I think you missed the part where I mentioned that I have tried your
    combination and I know that it works (look at the comments in my sample
    code). My question was more about why the other variations work when not
    using templates and if that code is illegal.

    Brian
    Brian Ross, Aug 17, 2003
    #3
  4. >
    > Hi,
    >
    > I think you missed the part where I mentioned that I have tried your
    > combination and I know that it works (look at the comments in my sample
    > code). My question was more about why the other variations work when not
    > using templates and if that code is illegal.
    >
    > Brian
    >


    You're right I missed the point, sorry about that. I surprised that 1 and 2
    work at all even for non-templates, so I don't think I should say any more.

    john
    John Harrison, Aug 17, 2003
    #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. Replies:
    3
    Views:
    832
    Peter_Julian
    Oct 10, 2005
  2. Angus
    Replies:
    1
    Views:
    2,609
  3. aaragon
    Replies:
    2
    Views:
    606
    James Kanze
    Nov 2, 2008
  4. Hicham Mouline
    Replies:
    0
    Views:
    423
    Hicham Mouline
    Apr 23, 2009
  5. Hicham Mouline
    Replies:
    1
    Views:
    406
    Michael DOUBEZ
    Apr 24, 2009
Loading...

Share This Page