【问题标题】:Explicit type conversion not allowed from "literal_string to "string storage pointer"不允许从“literal_string”到“字符串存储指针”的显式类型转换
【发布时间】:2018-04-26 12:35:10
【问题描述】:

我正在使用 truffle 开发虚拟测试合约,用于以下代码,

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" 的显式类型转换。"

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

请指教,这里怎么不正确。

【问题讨论】:

    标签: solidity truffle


    【解决方案1】:

    试试这个:

    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 的显式转换(因为该值已经是一个字符串)并导致您看到的编译错误。

    【讨论】:

      猜你喜欢
      • 2017-08-27
      • 1970-01-01
      • 2022-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多