solidity - 不允许从“literal_string”到“字符串存储指针”的显式类型转换
问题描述
我正在用松露开发虚拟测试合同,下面的代码,
pragma solidity ^0.4.17;
import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/SkillDevelopment.sol";
contract TestSkillDevelopment {
SkillDevelopment skillDevelopmentContract = SkillDevelopment(DeployedAddresses.SkillDevelopment());
function testSetStudentEnrollInfo() public {
skillDevelopmentContract.setStudentEnrollInfo("{\"Info\":[{\"id\":\"06bb05b8b3152b0323f4a9c07ae84f0c513b24946a8f8ec685af7cd763f7ffad\",\"studentId\":6532916,\"parenterId\":6187778,\"schemeId\":3853115,\"batchId\":3458926,\"instructorId\":1175723,\"trainingId\":2599715,\"studentSkillState\":1,\"state\":1}]}");
string expected = string("{\"Info\":[{\"id\":\"06bb05b8b3152b0323f4a9c07ae84f0c513b24946a8f8ec685af7cd763f7ffad\",\"studentId\":6532916,\"parenterId\":6187778,\"schemeId\":3853115,\"batchId\":3458926,\"instructorId\":1175723,\"trainingId\":2599715,\"studentSkillState\":1,\"state\":1}]}");
Assert.equal(skillDevelopmentContract.getStudentEnrollInfo(), expected, "The message should be set");
}
}
但出现错误
" TypeError: 不允许从 "literal_string 到 "string storage pointer" 的显式类型转换。"
在运行“松露测试”命令时。
请建议,这里怎么不正确。
解决方案
试试这个:
string memory expected = "{\"Info\":[{\"id\":\"06bb05b8b3152b0323f4a9c07ae84f0c513b24946a8f8ec685af7cd763f7ffad\",\"studentId\":6532916,\"parenterId\":6187778,\"schemeId\":3853115,\"batchId\":3458926,\"instructorId\":1175723,\"trainingId\":2599715,\"studentSkillState\":1,\"state\":1}]}";
a 的默认位置string
是storage
,这意味着您必须将其指向存储中的某个状态变量。切换到memory
解决该问题。最后,string
不需要显式转换为(因为该值已经是一个字符串)并导致您看到的编译错误。
推荐阅读
- azure - 使用 azure 函数通过弹出窗口或 twilio 短信通知 Azure cosmos db 更改
- r - 为什么 ifelse 或 if_else 在 purrr::accumulate 中不能按预期工作
- java - 即使具有相同的数据,断言也无法比较两个数据类
- mysql - 对 MySQL 表进行排序以在数据表上使用
- excel - “间接”引用循环中的组合框
- java - 如何确保 maven 项目的分发文件的名称与某个版本无关,而是始终包含最新的
- python-3.x - 使用 pytest 在控制台上禁用日志输出的打印
- javascript - 用户连接 Passport 节点时在导航栏中显示选项
- c# - 使用 Avalonia 绑定传递两个命令参数
- spring - 在 Spring Elasticsearch 中使用多个查询搜索多个字段