普通函数内,无法为【storage 类型结构体】进行赋值。

悦家二狗 悦家二狗 提出于 2021-06-20 16:13 5547 浏览

如果我把 channels 这个函数里面的 Channels storage c, 改为 memory 就可以正常赋值。

但是设置为 storage 就无法进行赋值了,很疑惑改成 storage就无法赋值了。

报错信息:this variable is of storage pointer type and can be accessed without prior assignment, which would lead to undefined behaviour .

直接翻译是:此变量是存储指针类型,可以在没有事先赋值的情况下访问,这将导致未定义的行为。

pragma solidity ^0.7.6;

contract ChannelControl {

struct Channels{ 
  bool exit;  
  uint256 index;
}

mapping(uint256 => mapping(uint256=> Channels)) public ChannelsMapping;

uint256 index = 0;

function channels() public{
	Channels storage c;
	c.exit = true;
	c.index = index;
	ChannelsMapping[index][index] = c;
	index ++;
}

2021062040445.png

最佳答案

2021-06-21 08:38

状态变量是全局存在的, 他要么是在函数的外边,要么是从你的mapping 里先获得值。 如:

Channels storage c = channelsMapping[1][2];
c.exit = true;
c.index = index;

或者使用memery .

Tiny熊

擅长:智能合约,以太坊 · 采纳率 33% · 回答于 2021-06-21 08:38

其它 0 个回答

写回答

你需要登录后才可以回答问题,登录