2017-11-12 9 views
1

私は頑強ではなく、簡単なコードで経験しています。 基本的には、契約内にデータを永続的に格納する構造体を作成したいだけです。ドキュメントによると、私はstorageが必要です。 しかし、代わりにmemoryを使用する場合を除いて、次のコードはコンパイルされません。どうして?高密度ストレージ構造体はコンパイルされていません

pragma solidity ^0.4.11; 
contract test { 

struct Selling { 
    address addr; 
    string name; 
    uint price; 
} 

mapping(string => Selling) selling; 

function sellName() constant returns (bool ok) 
{ 
    address a = 0x4c3032756d5884D4cAeb2F1eba52cDb79235C2CA; 
    Selling storage myStruct = Selling(a,"hey",12); 
} 
} 

私が手にエラーがこれです:あなたが最初myStructインスタンスを作成するとき

ERROR: 
browser/test.sol:16:9: TypeError: Type struct test.Selling memory is not implicitly convertible to expected type struct test.Selling storage pointer. 
     Selling storage myStruct = Selling(a,"hey",12); 
     ^--------------------------------------------^ 

答えて

1

、それはメモリ内に作成して、ストレージに書き込まれます(あなたがsellingにオブジェクトを置くと仮定マップし、あなたのメソッドconstant)を宣言しないでください)。別の関数でマップから項目を取得する場合は、その変数を記憶変数として宣言します。

詳細については、Ethereum StackExchangeのthis explanationを参照してください。 Solidity documentationには、変数の格納方法といつmemorystorageを使用するかについての非常に良い説明もあります。

関連する問題