简体   繁体   English

预期的主要表达(固体)

[英]Expected Primary Expression (Solidity)

I am creating a simple smart contract, however, I am getting an error on my last function ("ViewNotes") stating that the compiler was "Expected Primary Expression"? 我正在创建一个简单的智能合约,但是,上一个函数(“ ViewNotes”)出现错误,指出编译器是“期望的主表达式”? Can I not check the value at a mapping (of address => string) against the value 0 ? 我不能在一个映射(地址=>字符串)对0值检查值?

My code: 我的代码:

pragma solidity ^0.4.4;

contract Logistics{

address public owner;
mapping(address => string) notes;

modifier onlyOwner() {
    require(msg.sender == owner);
    _;
}

constructor(address genesis) public {
   owner = genesis;
}

function sign(string signedNote) public onlyOwner{
        notes[owner] = signedNote; //gaurenteed that msg.sender == owner
}

function transferOwnership(address nuOwner) onlyOwner {
    owner = nuOwner;
}

function viewNotes(address participant) public returns(string){ // signed note on success nothing on fail
    if(notes[participant] !== 0){
        return (notes(participant));   
    }
}

} }

There are a couple issues. 有几个问题。 The primary issue is that you misspelled != . 主要问题是您拼错了!= (You have an extra equals sign. !== is an operator in JavaScript, but not in Solidity.) (您有一个额外的等号。 !==是JavaScript中的运算符,但不是Solidity中的运算符。)

Once you fix that, you'll find that you can't compare a string to the number 0 . 解决此问题后,您将发现无法将string与数字0进行比较。 You probably want to check the string's length? 您可能要检查字符串的长度? You'll need to cast to bytes to do that: 您需要强制转换为bytes才能执行此操作:

function viewNotes(address participant) public returns (string) {
    if (bytes(notes[participant]).length != 0) {
        return notes[participant];
    }
}

That said, I believe this is probably equivalent to just: 也就是说,我相信这可能等同于:

function viewNotes(address participant) public returns (string) {
    return notes[participant];
}

And you could instead just make notes public : 相反,您可以public notes

mapping(address => string) public notes;

That way, Solidity will generate a getter function for you, and people can just call notes(addr) , making viewNotes redundant. 这样,Solidity将为您生成一个getter函数,人们可以只调用notes(addr) ,从而使viewNotes多余。

Fixing up a couple other warnings, getting rid of the modifier in favor of a direct ownership check, and assigning initial ownership to the deployer, here's my take on the contract: 修正了一些其他警告,摆脱了直接支持所有权检查的修饰符,并将初始所有权分配给部署者,这是我对合同的看法:

pragma solidity ^0.4.24;

contract Logistics{
    address public owner = msg.sender;
    mapping(address => string) public notes;

    function sign(string note) public {
        require(msg.sender == owner);
        notes[owner] = note;
    }

    function transferOwnership(address newOwner) public {
        require(msg.sender == owner);
        owner = newOwner;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM