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

说明

在这里插入图片描述

编译出错的提示:方法的参数期望的是一个左值!一般像我这类不熟悉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"));
    }
}

参考文档

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值