472,808 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,808 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 21078
"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() { } ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.