error: invalid initialization of non-const reference of type 'QVariant&' from an rvalue of type 'QVariant' QVariant& variant = indexta(Qt::UserRol.dae + 1); ~~~~~~~~~~^~~~~~~~~~~~~~~~~~
时间: 2023-07-03 18:31:38 浏览: 368
这个错误提示是因为你尝试将一个右值(rvalue)赋值给一个非 const 引用(non-const reference)的变量。在这里,你声明了一个 QVariant 类型的引用变量 variant,并尝试将一个表达式 `indexta(Qt::UserRol.dae + 1)` 的结果赋值给它。但是,这个表达式返回的是一个临时对象,也就是一个右值,不能直接赋值给一个非 const 引用变量。
你可以通过将 variant 的类型改为 const QVariant& 或者将表达式的结果保存在一个变量中,然后再将这个变量的值赋给 variant 来解决这个问题。例如:
```cpp
const QVariant& variant = indexta(Qt::UserRole.dae + 1);
```
或者
```cpp
QVariant tmp = indexta(Qt::UserRole.dae + 1);
QVariant& variant = tmp;
```
这样做可以避免编译错误。
相关问题
error: invalid initialization of non-const reference of type 'uint& {aka unsigned int&}' from an rvalue of type 'uint {aka unsigned int}' bool init(uint& iMonitorIndex = 0, RECT& Rect = { 0,0,0,0 }); ^
这个错误的原因是函数的参数是非常量引用(non-const reference),而你传递的是一个右值(rvalue)。右值是指临时对象或字面量,它们不能被修改,因此不能被传递给非常量引用。
你需要将参数改为常量引用(const reference)或传递一个具有名称的变量作为参数,因为可以将具有名称的变量作为左值(lvalue)传递给非常量引用。
例如,你可以这样调用该函数:
```
uint iMonitorIndex = 0;
RECT rect = {0, 0, 0, 0};
init(iMonitorIndex, rect);
```
或者将参数改为常量引用:
```
bool init(const uint& iMonitorIndex = 0, const RECT& Rect = {0, 0, 0, 0});
```
[Error] invalid initialization of non-const reference of type 'LinkQueue&' from an rvalue of type 'LinkQueue*'
这个错误信息 "Error: invalid initialization of non-const reference of type 'LinkQueue&' from an rvalue of type 'LinkQueue*'" 出现在 C++ 编程中,意味着你在尝试初始化一个引用(reference)时遇到了问题。`LinkQueue&` 表示对 LinkQueue 类型的引用,而 `LinkQueue*` 是指向 LinkQueue 的指针。
C++ 中,引用必须绑定到某个已存在的对象上,而不能直接从指针创建引用。当你试图从一个指针(可以看作临时的对象)去初始化引用时,因为指针不是具体的对象实例,所以会导致这样的错误。
例如,下面的代码可能会引发此类错误:
```cpp
LinkQueue* pq = new LinkQueue(); // 创建一个指针
LinkQueue& ref = *pq; // 错误:不能从指针创建引用,应该解引用后再初始化引用
```
正确的做法应该是先将指针解引用,再用解引用后的对象来初始化引用:
```cpp
LinkQueue* pq = new LinkQueue(); // 创建一个指针
LinkQueue tmp = *pq; // 先复制指向的对象
LinkQueue& ref = tmp; // 现在可以安全地初始化引用
```
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)