"expected constructor, destructor, or type conversion before '->' token" using a Singleton

Discussion in 'C++' started by Damien, Dec 12, 2006.

  1. Damien

    Damien Guest

    Hi all,

    I'm using a pretty standard C++ Singleton class, as below:

    template <typename T>
    class Singleton
    {
    public:
    static T* Instance()
    {
    static T instance_;
    return &instance_;
    }

    private:
    Singleton(); // ctor hidden
    ~Singleton(); // dtor hidden
    Singleton(Singleton const&); // copy ctor hidden
    Singleton& operator=(Singleton const&); // assign op hidden
    };

    Trying to use it on something really simple is giving me headaches:

    class DoIt
    {
    public:
    DoIt(){}
    ~DoIt(){}

    void DoSomething(){}
    };

    int main()
    {
    DoIt* doit = Singleton<DoIt>::Instance();
    doit->DoSomething();
    }

    I get the compiler error "error: expected constructor, destructor, or
    type conversion before '->' token" on the doit->DoSomething(); line. I
    can't see how doit can be name-dependent because there's no typedef'ing
    associated with it inside the Singleton, unless the static has
    something to do with it. Anyone got any ideas?

    Damien
    Damien, Dec 12, 2006
    #1
    1. Advertising

  2. "Damien" <> wrote in message
    news:...
    : I'm using a pretty standard C++ Singleton class, as below:
    :
    : template <typename T>
    : class Singleton
    : {
    : public:
    : static T* Instance()
    : {
    : static T instance_;
    : return &instance_;
    : }
    :
    : private:
    : Singleton(); // ctor hidden
    : ~Singleton(); // dtor hidden
    : Singleton(Singleton const&); // copy ctor hidden
    : Singleton& operator=(Singleton const&); // assign op hidden
    : };
    :
    : Trying to use it on something really simple is giving me headaches:
    :
    : class DoIt
    : {
    : public:
    : DoIt(){}
    : ~DoIt(){}
    :
    : void DoSomething(){}
    : };
    :
    : int main()
    : {
    : DoIt* doit = Singleton<DoIt>::Instance();
    : doit->DoSomething();
    : }
    :
    : I get the compiler error "error: expected constructor, destructor, or
    : type conversion before '->' token" on the doit->DoSomething(); line.
    I
    : can't see how doit can be name-dependent because there's no
    typedef'ing
    : associated with it inside the Singleton, unless the static has
    : something to do with it. Anyone got any ideas?
    Well, the code you posted compiles "as is" in both Comeau and VC8.1.
    However, the design of your singleton seems weird to me (was it an
    artificial example posted for demonstration purposes?), and it does
    not prevent the creation of additional DoIt instances.

    Your "Singleton" class could be replaced with a single function:
    template<class T>
    T& single_instance() { static T instance; return instance; }

    Usage:
    int main()
    {
    DoIt& doit = single_instance<DoIt>();
    doit.DoSomething();
    }

    An online search will probably bring a few existing singleton
    implementations, up to Andrei Alexandrescu's policy-based design.
    But if the 'static function member' does what you need, I personally
    wouldn't bother using a Singleton template to implement it.

    hth -Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Brainbench MVP for C++ <> http://www.brainbench.com
    Ivan Vecerina, Dec 12, 2006
    #2
    1. Advertising

  3. Damien

    Guest

    On Dec 12, 7:29 am, "Damien" <> wrote:
    > Hi all,
    >
    > I'm using a pretty standard C++ Singleton class, as below:
    >
    > template <typename T>
    > class Singleton
    > {
    > public:
    > static T* Instance()
    > {
    > static T instance_;
    > return &instance_;
    > }
    >
    > private:
    > Singleton(); // ctor hidden
    > ~Singleton(); // dtor hidden
    > Singleton(Singleton const&); // copy ctor hidden
    > Singleton& operator=(Singleton const&); // assign op hidden
    > };
    >
    > Trying to use it on something really simple is giving me headaches:
    >
    > class DoIt
    > {
    > public:
    > DoIt(){}
    > ~DoIt(){}
    >
    > void DoSomething(){}
    > };
    >
    > int main()
    > {
    > DoIt* doit = Singleton<DoIt>::Instance();
    > doit->DoSomething();
    > }
    >
    > I get the compiler error "error: expected constructor, destructor, or
    > type conversion before '->' token" on the doit->DoSomething(); line. I
    > can't see how doit can be name-dependent because there's no typedef'ing
    > associated with it inside the Singleton, unless the static has
    > something to do with it. Anyone got any ideas?


    You sure this is the code you are trying to compile? It works fine for
    me both in VC8 and Comeau Online.

    --
    Erik Wikström
    , Dec 12, 2006
    #3
  4. Damien

    Damien Guest

    I should have said the compiler is gcc 4.1.1. I know it's not a
    perfect Singleton design, it's the error message I want to understand.

    DAmien

    On Dec 12, 12:30 am, ""
    <> wrote:
    > On Dec 12, 7:29 am, "Damien" <> wrote:
    >
    >
    >
    > > Hi all,

    >
    > > I'm using a pretty standard C++ Singleton class, as below:

    >
    > > template <typename T>
    > > class Singleton
    > > {
    > > public:
    > > static T* Instance()
    > > {
    > > static T instance_;
    > > return &instance_;
    > > }

    >
    > > private:
    > > Singleton(); // ctor hidden
    > > ~Singleton(); // dtor hidden
    > > Singleton(Singleton const&); // copy ctor hidden
    > > Singleton& operator=(Singleton const&); // assign op hidden
    > > };

    >
    > > Trying to use it on something really simple is giving me headaches:

    >
    > > class DoIt
    > > {
    > > public:
    > > DoIt(){}
    > > ~DoIt(){}

    >
    > > void DoSomething(){}
    > > };

    >
    > > int main()
    > > {
    > > DoIt* doit = Singleton<DoIt>::Instance();
    > > doit->DoSomething();
    > > }

    >
    > > I get the compiler error "error: expected constructor, destructor, or
    > > type conversion before '->' token" on the doit->DoSomething(); line. I
    > > can't see how doit can be name-dependent because there's no typedef'ing
    > > associated with it inside the Singleton, unless the static has
    > > something to do with it. Anyone got any ideas?You sure this is the code you are trying to compile? It works fine for

    > me both in VC8 and Comeau Online.
    >
    > --
    > Erik Wikström
    Damien, Dec 12, 2006
    #4
  5. Damien

    Lionel B Guest

    Re: "expected constructor, destructor, or type conversion before'->' token" using a Singleton

    On Tue, 12 Dec 2006 06:11:22 -0800, Damien wrote:

    (Please don't top-post)

    > On Dec 12, 12:30 am, ""
    > <> wrote:
    >> On Dec 12, 7:29 am, "Damien" <> wrote:
    >>
    >>
    >> > Hi all,

    >>
    >> > I'm using a pretty standard C++ Singleton class, as below:

    >>
    >> > template <typename T>
    >> > class Singleton
    >> > {
    >> > public:
    >> > static T* Instance()
    >> > {
    >> > static T instance_;
    >> > return &instance_;
    >> > }

    >>
    >> > private:
    >> > Singleton(); // ctor hidden
    >> > ~Singleton(); // dtor hidden
    >> > Singleton(Singleton const&); // copy ctor hidden
    >> > Singleton& operator=(Singleton const&); // assign op hidden
    >> > };

    >>
    >> > Trying to use it on something really simple is giving me headaches:

    >>
    >> > class DoIt
    >> > {
    >> > public:
    >> > DoIt(){}
    >> > ~DoIt(){}

    >>
    >> > void DoSomething(){}
    >> > };

    >>
    >> > int main()
    >> > {
    >> > DoIt* doit = Singleton<DoIt>::Instance();
    >> > doit->DoSomething();
    >> > }

    >>
    >> > I get the compiler error "error: expected constructor, destructor, or
    >> > type conversion before '->' token" on the doit->DoSomething(); line. I
    >> > can't see how doit can be name-dependent because there's no typedef'ing
    >> > associated with it inside the Singleton, unless the static has
    >> > something to do with it. Anyone got any ideas?

    >>
    >> You sure this is the code you are trying to compile? It works fine for
    >> me both in VC8 and Comeau Online.

    >
    > I should have said the compiler is gcc 4.1.1. I know it's not a perfect
    > Singleton design, it's the error message I want to understand.


    Compiles ok for me here on gcc 4.1.1 (linux x86_64). Sure there's not
    something you're not telling us?

    --
    Lionel B
    Lionel B, Dec 12, 2006
    #5
  6. Damien

    Damien Guest

    Argh. My bad. I'm using SCons and I didn't update something in a
    build file, so it wasn't compiling my code above.

    The code above is fine. What was broken was a pair of macros
    (%$#$#*&!!!) that put an equivalent of doit.DoSomething() outside the
    flow of control, so of course it's a declaration and the compiler
    correctly complains.

    Thank you all and sorry to waste your time,

    Damien
    Damien, Dec 13, 2006
    #6
    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. Proton Projects - Moin

    Singleton - Whether Cloneable overrides Singleton

    Proton Projects - Moin, Mar 26, 2007, in forum: Java
    Replies:
    4
    Views:
    3,220
    Proton Projects - Moin
    Mar 27, 2007
  2. Replies:
    5
    Views:
    23,666
  3. Wilhelm
    Replies:
    1
    Views:
    158
  4. Trans
    Replies:
    12
    Views:
    268
    Robert Klemme
    Sep 14, 2007
  5. Paul McMahon
    Replies:
    3
    Views:
    197
    David A. Black
    Jun 9, 2008
Loading...

Share This Page