Thread: Error: initialization of non-const reference type

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    2

    Red face Error: initialization of non-const reference type

    Hi!

    I have been thinking about this one the whole night but couldn't figure it out. I guess it was my bad day (or night).

    So, I have written the following code (it does nothing useful, it is just for the illustration):

    Code:
    #include <iostream>
    using namespace std;
    
    class A {
      public:
      int* a;
      void change(int* & tmp) {a=tmp;}
      int* get() {return a;}
    };
    
    int main() {
      A temp;
      int* buff=new int(42);
      temp.change(buff);
      temp.change(temp.a);
      temp.change(temp.get());  //ERROR: Initialization of non-const reference type 'int *&', from rvalue of type 'int *', in passing argument 1 of 'A::change(int *&)'
      
      cout << temp.get() << endl;
      cout << temp.a << endl;
      
      return 0;
    }
    So I need to pass a pointer to a method by reference! It works fine in the line
    temp.change(temp.a);
    but when I use the get() method instead of just a:
    temp.change(temp.get());
    I get an error.

    Why???? I mean, both temp.a and temp.get() should return the same thing, shouldn't they???

    Now, I could have solved it by declaring the method A::change like this:
    void change(int* const & tmp) {a=tmp;}

    BUT I WANT TO BE ABLE TO CHANGE the variable tmp in this method. So the solution with const doesn't work for me.

    Any tips, explanations ?

    Thank you!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I mean, both temp.a and temp.get() should return the same thing
    They're the same thing by value (namely, an address), but a function return can only exist as an rvalue, and a reference is usually applied to an lvalue.

    So what's an lvalue and rvalue?
    left and right basically, as in left-hand side of an expression, or the right-hand side.

    A variable can exist as either, so you can say
    var = something; // var is lvalue
    something = var; // var is rvalue

    But for a function return, it must be an rvalue
    something = function(); // rvalue

    You can't for example say
    function() = something; // nope, it's not an lvalue

    That is basically what the error message is getting at - it's trying to create a reference to an lvalue, and all you have is an rvalue.

    My simplistic work-around is
    int *dummy = temp.get();
    temp.change(dummy);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    but a function return can only exist as an rvalue
    This is c++ not c. c++ has references. A function may return a reference and hence act as an lvalue.

    Now onto your problem.....

    int* get()

    This returns a pointer by value.....

    void change(int* & tmp)

    This takes a pointer by reference.

    temp.change(temp.get());

    This takes a pointer by reference but the pointer you give it is actually a temporary copy of the return value of temp.get(). The c++ standard says that you cant reference a temporary unless its const. Obviously this cant be const because you wish to change it so simple solution split the statement into 2 statements and use a named object instead of a temporary.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM