-
February 11th, 2013, 09:51 AM
#1
error: invalid initialization of non-const reference of type ...
Hello,
I am tryign to use the Singleton design patterno on Linux, but it is not working:
in the .h I have:
Code:
public:
static ErrorH& Instance();
private:
ErrorH() {};
in the .cpp I have:
Code:
ErrorH& ErrorH::Instance()
{
static ErrorH& self = ErrorH();
return self;
}
The errors I get are:
Code:
g++ --g -c ErrorH.cpp -o ErrorH.o
ErrorH.cpp: In static member function ‘static ErrorH& ErrorH::Instance()’:
ErrorH.cpp:9: error: invalid initialization of non-const reference of type ‘ErrorH&’ from a temporary of type ‘ErrorH’
make: *** [ErrorH.o] Error 1
This code works on Windows, how can I get it to work on Linux?
Regards.
-
February 11th, 2013, 02:27 PM
#2
Re: error: invalid initialization of non-const reference of type ...
Originally Posted by ekhule
Hello,
I am tryign to use the Singleton design patterno on Linux, but it is not working:
It has nothing to do with Linux. C++ doesn't know what operating system is being used if the issue is a language issue.
The compiler you're using is gcc, not Linux -- Linux is an operating system. They make gcc for Linux, Windows, and a whole host of other operating systems -- you would get the same error, regardless of the OS used.
The gcc compiler is correct -- A non-const reference is supposed to be assigned to a non-const item, in other words, how can you create a reference to something that won't exist? The ErrorH return value is a temporary, meaning it goes "poof" into a puff of smoke and disappears as soon as that line is finished executing. So now you have a reference referring to ...? What exactly ...?
You assign references to living items that stay alive for the time the reference is being used. The Microsoft compiler is not correct, but accepts this syntax anyway. However, do you have your warnings set to 4 (W4) for the MS compiler? I believe you do get the warning that what you're doing is non-standard usage (even though it is officially an error).
Regards,
Paul McKenzie
Last edited by Paul McKenzie; February 11th, 2013 at 02:30 PM.
-
February 11th, 2013, 02:33 PM
#3
Re: error: invalid initialization of non-const reference of type ...
Originally Posted by ekhule
in the .cpp I have:
Code:
ErrorH& ErrorH::Instance()
{
static ErrorH& self = ErrorH();
return self;
}
You need to create a static instance, not a static reference.
Code:
ErrorH& ErrorH::Instance()
{
static ErrorH self;
return self;
}
Originally Posted by ekhule
This code works on Windows, how can I get it to work on Linux?
It doesn't work on Windows. By default VC++ allows taking a reference to a temporary object, which is not legal C++. All you end up with is garbage. VC++ gives a warning (level 4) for this. It's best to make this in error using #pragma, e.g. in your stdafx.h.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|