Thread: Problem converting std::vector into a reference

  1. #16
    Registered User
    Join Date
    Oct 2005
    Posts
    88
    Mario F., your suggestion worked perfectly. God knows why, but it did.

    Actually, since I've had many, many error related to references based on temporary variables, I don't think I'd be that suprised about the compiler complaining about the 'stmt' reference being an error but, like you say, I have no idea why MSVC++ thought the first parameter was the error...

    Thanks for your help though; 'twas wrecking my head

  2. #17
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    > God knows why, but it did.

    Because you were calling your function like this:

    Code:
    mDBH.query( data, "asdosah" )
    "asdosah" is a string literal. String literals are of type const char[]. This means that there is the need for a conversion to take place. And according to the explanation of my previous post, a non const reference can only be initialized with an object of the exact same type. A const, on the other hand, doesn't need to.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #18
    Registered User
    Join Date
    Oct 2005
    Posts
    88
    Ahhhh. That makes perfect sense. I'm incredibly suprised that I hadn't noticed this before now, since I've used function calls like this before and seemingly 'missed' the very same error.

    The help is hugely welcome. Thanks.

  4. #19
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by drrngrvy
    Ahhhh. That makes perfect sense. I'm incredibly suprised that I hadn't noticed this before now, since I've used function calls like this before and seemingly 'missed' the very same error.

    The help is hugely welcome. Thanks.
    For future reference, it's an lvalue/rvalue thing. An lvalue is any expression that can appear on the left side of an assignment operator. For example:

    int x;
    x = 5;

    In the second statement, x is a valid lvalue, because it can appear on the left side of =. 5, on the other hand, is not a valid lvalue, as it can't:

    5 = x; // compiler error!

    A non-const reference MUST be a reference to an lvalue. A string literal is not an lvalue. Also, a non-const reference will never do type conversion -- so a reference to double can't take an int, for example.

    A const reference will accept anything, lvalue or rvalue, and it will do the same type conversions that would occur if you weren't using references.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with call by reference
    By zephyrcat in forum C Programming
    Replies: 2
    Last Post: 02-28-2008, 09:04 PM
  2. string to char converting problem
    By bergziege in forum C++ Programming
    Replies: 2
    Last Post: 08-07-2007, 03:37 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. GCC - Strange networking functions error.
    By maththeorylvr in forum Windows Programming
    Replies: 3
    Last Post: 04-05-2005, 12:00 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM