0

It's giving me an error telling me my reference variable hasn't been initialized. What does that mean, what's wrong with my code here?

/* ------ FUNCTION: NEW ENTRY ------------------------------------
----------------------------------------------------------------*/
void newEntry(char entries[], int index[])
{
    char buffer[256];
    char password[8];
    bool val = 0;
    int &length;

    cout << "\n------ New Entry ------------------------\n";
    cout << "Please enter your new password: ";
    cin >> buffer;

    val = valLength(buffer, password, length);

    while(val == 0)
    {
        cout << "\nYour password must be between 8 and 15 characters in length\n";
        cout << "Your password was " << length;
        val = valLength(buffer, password, length);

        cout << "\nPlease enter your new password: ";
        cin >> buffer;

    }


}
3
Contributors
6
Replies
17
Views
2 Years
Discussion Span
Last Post by Jjajangmyeon
Featured Replies
  • 1

    As the message says `int& length` is a reference (that's what the `&` means). You can not define a reference without initializing it. That is you need to do `int& length = foo;` where `foo` is an l-value (like a variable, an array access, a member access or a function … Read More

  • A reference is another name for some variable; when you use the reference, you're using that exact same other variable. A reference is an alias, if you like. `int &length;` In this case, what other variable is "length" another name for? None. It makes no sense. `int &length = 0;` … Read More

  • 1

    `int& length = 0;` does not work because 0 is not an l-value. As I said an l-value is something like a variable or an array access - something that can be assigned to. You can't assign to 0. Think of it this way: defining `length` as a reference means … Read More

1

As the message says int& length is a reference (that's what the & means). You can not define a reference without initializing it. That is you need to do int& length = foo; where foo is an l-value (like a variable, an array access, a member access or a function call that returns a reference).

Just writing int& length; is not allowed because then the compiler does not know what length is a reference to.

0

I'm still not clear, as I tried this

int &length = 0;

and I got yhe same error. What am I missing? (thanks for the help)

0

OH Wait, I don't use the & in the declaration only when passing it? I removed the & in the dec and now it seems to work fine.

1

A reference is another name for some variable; when you use the reference, you're using that exact same other variable. A reference is an alias, if you like.

int &length;
In this case, what other variable is "length" another name for? None. It makes no sense.

int &length = 0;
In this case, what other variable is "length" another name for? Still no other variable. It makes no sense.

Edited by Moschops

1

int& length = 0; does not work because 0 is not an l-value. As I said an l-value is something like a variable or an array access - something that can be assigned to. You can't assign to 0.

Think of it this way: defining length as a reference means "whenever I change length, I want the target of the reference to also change". That is when you write int& length = x; (where x is an int variable) and then length = 42;, the value of x will now also be 42. If you write int& length = 0; and then length = 42;, that would mean that now the value of 0 is now also 42. Of course that doesn't make any sense - the value of 0 can't be 42; the value of 0 is 0. So you're not allowed to write int& length = 0;.

I don't use the & in the declaration only when passing it?

You use & if you want length to be a reference to something, that is if you want something else to change whenever length changes. If that's not what you want, you don't use &.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.