" Expected '(' but got identifier " & " Parse error: mismatched input '{' expecting {';', '='} [undefined] "

" Expected '(' but got identifier " & " Parse error: mismatched input '{' expecting {';', '='} [undefined] "

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;

contract CrowdFunding 
{
    
    struct Investor
    {
        address addr;
        uint amount;
    }
    
    address public owner;       
    uint public NumOfInvestors; 
    uint public deadline;       
    string public status;       
    bool public isOver;         
    uint public goalAmount;     
    uint public totalAmount;    
    mapping (uint => Investor) public investors; 
    
    modifier onlyOwner () 
    {
        require(msg.sender == owner);
        _;
    }
    
    
    function crowdfunding(uint _duration, uint _goalAmount) public
    {
        owner == msg.sender;
        deadline = now + _duration;
        
        goalAmount = _goalAmount;
        status = "Funding";
        isOver = "false";
        NumOfInvestors = 0;
        totalAmount = 0;
    }
    
    
    function fund() payable public
    {
       
        require(!isOver);
        
        Investor inv = investors[NumOfInvestors++]; 
        inv.addr = msg.sender;                      
        inv.amount = msg.value;                     
        totalAmount += inv.amount;
    }
    
   
    function checkGoalReached () public onlyOwner 
    {
        
        
        require(!isOver);
        
        
        require(now >= deadline);
        
        if(totalAmount >= goalAmount) 
        {  
            status = "Campaign succeeded.";
            isOver = true;
            
            
            if(!owner.send(this.balance)) 
            {
                throw;
            }
            else 
            { 
                
                uint i = 0;
                status = "Campaign Failed.";
                isOver = true;
                
                while (i <= NumOfInvestors) 
                {
                    
                    if(!investors[i].addr.send(investors[i].amount)) 
                    {
                        throw;   
                    }
                    
                    i++;
                }
            }
        }
        
        function kill() public onlyOwner 
        {
            selfdestrct(owner);
        }

    }
}

我写了一个合约,但是函数 kill() 的第一行和第二行都出错了。

它在第一行显示“ Expected '(' but got identifier”,在第二行显示“ Parse error: mismatched input '{' expecting { ';' , '=' } [undefined] ”。

我仔细检查了我的代码结构,还阅读了一些来自 Whosebug 的文章。

对于第一行显示的问题,那些文章说可能会漏掉一个“{”或“}”。但我肯定没有错过。 (如果我真的错过了他们,我就是个傻瓜。

至于第二行显示的问题,我就是说不出为什么不对。 会不会是版本问题??

请帮助我。求助。

你的

        function kill() public onlyOwner 
        {
            selfdestrct(owner);
        }

在另一个函数里面,把它移到外面,一切正常。请检查闭合制动器 }。 此外,您还需要更正拼写错误 selfdestruct

这是工作代码:

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;

contract CrowdFunding 
{
    
    struct Investor
    {
        address payable addr;
        uint amount;
    }
    
    address payable public owner;       
    uint public NumOfInvestors; 
    uint public deadline;       
    string public status;       
    bool public isOver;         
    uint public goalAmount;     
    uint public totalAmount;    
    mapping (uint => Investor) public investors; 
    
    modifier onlyOwner () 
    {
        require(msg.sender == owner);
        _;
    }
    
    
    function crowdfunding(uint _duration, uint _goalAmount) public
    {
        owner == msg.sender;
        deadline = block.timestamp + _duration;
        
        goalAmount = _goalAmount;
        status = "Funding";
        isOver = false;
        NumOfInvestors = 0;
        totalAmount = 0;
    }
    
    
    function fund() payable public
    {
       
        require(!isOver);
        
        Investor storage inv = investors[NumOfInvestors++]; 
        inv.addr = payable(msg.sender);                      
        inv.amount = msg.value;                     
        totalAmount += inv.amount;
    }
    
   
    function checkGoalReached () public onlyOwner 
    {
        
        
        require(!isOver);
        
        
        require(block.timestamp >= deadline);
        
        if(totalAmount >= goalAmount) 
        {  
            status = "Campaign succeeded.";
            isOver = true;
            
            address payable self = payable(address(this));
            uint256 balance = self.balance;
            if(!owner.send(balance)) 
            {
                revert();
            }
            else 
            { 
                
                uint i = 0;
                status = "Campaign Failed.";
                isOver = true;
                
                while (i <= NumOfInvestors) 
                {
                    
                    if(!investors[i].addr.send(investors[i].amount)) 
                    {
                        revert();   
                    }
                    
                    i++;
                }
            }
        }
        
        

    }
        function kill() public onlyOwner 
        {
            selfdestruct(owner);
        }
}