CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2009
    Posts
    166

    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.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: error: invalid initialization of non-const reference of type ...

    Quote Originally Posted by ekhule View Post
    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.

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: error: invalid initialization of non-const reference of type ...

    Quote Originally Posted by ekhule View Post
    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;
    }
    Quote Originally Posted by ekhule View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured