[Ethereum] Type uint256 is not implicitly convertible to expected type int256

compilererrorsolidity

I'm trying to compile the following keep getting an error, even though used this before in second code and its working fine.
Simply trying to change the temp to the new temperature set by the user, so that I could call/get it from Solidity.
Any help on how to fix the error or achieve what I want?

Code:

uint256 public temp;
constructor() public payable {
.
.
.
.
  temp = 15;
    }
event DataCommited( int256 T, string _L);

function commitTask(int256 _temp, string memory _location) public inState(State.Created) payable{
require (keccak256(abi.encodePacked(location)) == keccak256(abi.encodePacked(_location)));
require(_temp < 35 && _temp > -5, "Temperature out of range");
dataCount += 1;

if(dataCount == rewardNum)
        {
            msg.sender.transfer(reward);
            emit TaskDone();
        }

        emit DataCommited( _temp, _location);
        _temp = temp;

    }

Working code without an error:

uint256 age;
function setInstructor(string memory _fName, uint256 _age) onlyOwner public  {
   fName = _fName;
   age = _age;
   emit Instructor (_fName, _age);
}

Best Answer

You have uint256 temp and int256 _temp.

In _temp = temp, you are trying to assign the value of a uint256 variable to an int256 variable.

The compilation error tells you that you cannot use an implicit conversion, so use an explicit conversion instead:

_temp = int256(temp);