473,765 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"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(Singl eton const&); // copy ctor hidden
Singleton& operator=(Singl eton 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 21160
"Damien" <da************ @gmail.comwrote in message
news:11******** **************@ l12g2000cwl.goo glegroups.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(Singl eton const&); // copy ctor hidden
: Singleton& operator=(Singl eton 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.DoSomethin g();
}

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(Singl eton const&); // copy ctor hidden
Singleton& operator=(Singl eton 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.sewro te:
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(Singl eton const&); // copy ctor hidden
Singleton& operator=(Singl eton 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.sewro te:
>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(Singl eton const&); // copy ctor hidden
Singleton& operator=(Singl eton 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.DoSomethin g() 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
11737
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 instance of A is constructed: # a.h class A {
19
3580
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 one web presentation tier (Foo.WebFiles). The data tier shall only be accessible thru the business tier so I do NOT want a reference to the data tier in the presentation tier. In the business tier I have a class with the name CategoryItem that...
5
9665
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 throwing an exception. Is there a plan at Microsoft VB .NET team to include a keyword equivalent to "as"?
3
3193
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 missing from the code? Thanks - Jon Private Sub MakeParentTable() ' Create a new DataTable. Dim myDataTable As Datatable = New Datatable("ParentTable")
4
1896
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 much with C++ - I kind of mostly abandoned C++ some time ago, when I coded a project in C++, and some of my coworkers refused to use it -because- it was in C++.
9
2070
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
47699
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 same error on line 186. This same HTML and JavaScript works perfectly in Firefox and Opera. How can I make the code work in IE?
5
3345
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
2058
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 to use elsewhere. For example, suppose you have a class Note. This class stores some text, as a linked list of lines of text. The destructor runs as follows: Note::~Note() {
1
3475
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() { } // A virtual destructor
0
10168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10008
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9837
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8833
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.