Home  >  Article  >  Backend Development  >  How to solve the C++ compilation error: 'invalid initialization of reference of type 'type&' from expression of type 'type''?

How to solve the C++ compilation error: 'invalid initialization of reference of type 'type&' from expression of type 'type''?

王林
王林Original
2023-08-25 23:43:453204browse

解决C++编译错误:\'invalid initialization of reference of type \'type&\' from expression of type \'type\'\',如何解决?

Solution to C compilation error: 'invalid initialization of reference of type 'type&' from expression of type 'type'', how to solve it?

Problem background:
In C programming, we sometimes encounter compilation errors. One of them is the error message "invalid initialization of reference of type 'type&' from expression of type 'type'", that is, a type mismatch occurs when initializing data of reference type.

The cause of this error is an attempt to initialize an unmodifiable temporary object or literal to a non-const reference variable. The nature of reference types in C requires that the referenced object must have constant existence.

Solution:

  1. Correctly initialize the reference variable
    To solve this error, when initializing the reference variable, you need to ensure that the referenced object is a modifiable lvalue. An lvalue is a persistent object whose value can be accessed by name and modified. The following is a code example:
int main() {
    int value = 10;
    int& ref = value; // 正确示例:将一个可修改的lvalue赋给引用变量
    return 0;
}
  1. Avoid initializing reference variables to literal values
    If you try to initialize a literal value to a non-const reference variable, the compiler will issue the above error message. Because literals are temporary objects, their value cannot be modified by name. Here is an example of code that will cause the error:
int main() {
    int& ref = 10; // 错误示例:试图将字面值初始化为非常量引用变量
    return 0;
}

The correct approach is to save the literal value in a variable with constant existence and then assign it to the reference variable. The code example is as follows:

int main() {
    int value = 10;
    const int& ref = value; // 正确示例:将一个具有恒定存在性的变量的值赋给引用变量
    return 0;
}
  1. Avoid assigning a constant to a non-const reference variable
    A similar error will result if you try to assign a constant to a non-const reference variable. Because a constant is immutable, its value cannot be modified by referencing a variable. Here is an example of code that will cause the error:
int main() {
    const int value = 10;
    int& ref = value; // 错误示例:试图将常量赋给非常量引用变量
    return 0;
}

The correct approach is to assign the constant to a non-const variable with constant existence and assign it to a constant reference variable. The code example is as follows:

int main() {
    const int value = 10;
    const int& ref = value; // 正确示例:将一个常量赋给常量引用变量
    return 0;
}

Conclusion:
In C programming, when we encounter the compilation error "invalid initialization of reference of type 'type&' from expression of type 'type'", we need to pay attention to the reference The nature of the type requires that the referenced object be a modifiable lvalue. Avoid assigning temporary objects or literal values ​​to non-const reference variables, and use const references when possible to handle constant objects. By properly initializing the reference variables, we were able to resolve this compilation error.

The above is the detailed content of How to solve the C++ compilation error: 'invalid initialization of reference of type 'type&' from expression of type 'type''?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn