不允许从 "literal_string to " 字符串存储指针进行显式类型转换”

Explicit type conversion not allowed from "literal_string to "string storage pointer"

我正在为下面的代码开发与松露结合的虚拟测试合约,

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: Explicit type conversion not allowed from "literal_string to "string storage pointer"."

while 运行 "truffle test" 命令。

请指教,这里怎么不对

试试这个:

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

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