首页 > 解决方案 > 不允许从“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" 的显式类型转换。"

在运行“松露测试”命令时。

请建议,这里怎么不正确。

标签: soliditytruffle

解决方案


试试这个:

string memory expected = "{\"Info\":[{\"id\":\"06bb05b8b3152b0323f4a9c07ae84f0c513b24946a8f8ec685af7cd763f7ffad\",\"studentId\":6532916,\"parenterId\":6187778,\"schemeId\":3853115,\"batchId\":3458926,\"instructorId\":1175723,\"trainingId\":2599715,\"studentSkillState\":1,\"state\":1}]}";

a 的默认位置stringstorage,这意味着您必须将其指向存储中的某个状态变量。切换到memory解决该问题。最后,string不需要显式转换为(因为该值已经是一个字符串)并导致您看到的编译错误。


推荐阅读