473,326 Members | 2,148 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

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

Dec 12 '06 #1
5 21132
"Damien" <da************@gmail.comwrote in message
news:11**********************@l12g2000cwl.googlegr oups.com...
: 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
Dec 12 '06 #2
On Dec 12, 7:29 am, "Damien" <damien.hock...@gmail.comwrote:
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 '06 #3
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, "eri...@student.chalmers.se"
<eri...@student.chalmers.sewrote:
On Dec 12, 7:29 am, "Damien" <damien.hock...@gmail.comwrote:
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 '06 #4
On Tue, 12 Dec 2006 06:11:22 -0800, Damien wrote:

(Please don't top-post)
On Dec 12, 12:30 am, "eri...@student.chalmers.se"
<eri...@student.chalmers.sewrote:
>On Dec 12, 7:29 am, "Damien" <damien.hock...@gmail.comwrote:

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
Dec 12 '06 #5
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

Dec 13 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jahn Otto Næsgaard Andersen | last post by:
Hi, I have a class A with a static pointer to some data. The pointer is static because I want to access the data from all instances of the class. The data itself is allocated when the first...
19
by: Martin Oddman | last post by:
Hi, I have a compiling problem. Please take a look at the code below. I have an application that is built upon three tiers: one data tier (Foo.DataManager), one business tier (Foo.Kernel) and...
5
by: Hayato Iriumi | last post by:
When converting a type to another using CType and if the type conversion fails, it throw an exception. However, in C#, there is a keyword "as" which only makes the variable Nothing (null) without...
3
by: Jon | last post by:
I'm learning about datatables. When using the example provided by MS in the ..NET Framework Class Library for DATATABLE (see below) I get an error on line 3 that says "Type expected". Is something...
4
by: Dan Stromberg | last post by:
Hi folks. I'm working on building some software, some of which is written in C++, for a researcher here at the University. I have an extensive background in C and python, but I haven't done...
9
by: Joseph Turian | last post by:
Consider this code snippet which doesn't compile: struct DebugOptions { }; class Debug { public: Debug(const DebugOptions options) { _options = options; } private:
16
by: Steve Chapel | last post by:
When I load the page <https://bugzilla.mozilla.org/attachment.cgi?id=237739with Internet Explorer 7 RC 1, I get the error "Object Expected" at line 174. When I click on the button, I also get the...
5
by: Frederick Gotham | last post by:
If we have a simple class such as follows: #include <string> struct MyStruct { std::string member; MyStruct(unsigned const i) {
8
by: gw7rib | last post by:
I've been bitten twice now by the same bug, and so I thought I would draw it to people's attention to try to save others the problems I've had. The bug arises when you copy code from a destructor...
1
by: Bart Simpson | last post by:
Can anyone explain the concept of "slicing" with respect to the "virtual constructor" idiom as explain at parashift ? From parashift: class Shape { public: virtual ~Shape() { } ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.