当前位置: 代码迷 >> 综合 >> candidate function not viable: expects an l-value for 1st argument
  详细解决方案

candidate function not viable: expects an l-value for 1st argument

热度:67   发布时间:2024-01-09 23:23:23.0

说明

在这里插入图片描述

编译出错的提示:方法的参数期望的是一个左值!一般像我这类不熟悉C++的又要混用多种编程语言的人是有可能遇到这个类基础的问题。
还有我发现在复杂的函数定义然后在Android studio的开发环境下,出错的提示会有些不一样,但基本上会看出是左右值的问题
在这里插入图片描述
这里的case会扯到C++的引用参数和C++的左值与右值的知识点了,在网络搜索了下相关的学习资料并整理在文章最后,同大家一起分享与共同学习之

示例代码

C++的代码(IDE是CLion)

#include <iostream>
#include <string>using namespace std;void testLRValue(string& name) {cout << "name = " << name << endl;
}int main(int argc, char* argv[]) {string test = "luogw";const char * p = test.c_str();string temp = std::string(p);testLRValue(temp);//下面这句会编译出错,提示“candidate function not viable: expects an l-value for 1st argument”testLRValue(std::string(p));cout << "temp = " << temp << endl;return   0;
}
package com.company;public class Main {public static void testLRValue(String name) {System.out.println(String.format("name = %s", name));}public static void main(String[] args) {String temp = "eric";testLRValue(temp);//在java世界里引用的使用很简单,因为没有引用,指针这类东西与它们的使用区别等testLRValue(new String("lily"));}
}

参考文档

  • Understanding lvalues and rvalues in C and C++
  • Lvalue Rvalue and Their References With Example in C++
  相关解决方案