0
votes

I am trying to create a struct and add mapping in such a way that it can be retrieved later on using its address using the below code.

pragma solidity ^0.8.0;

contract Courses {
    
    struct Instructor {
        uint age;
        string fName;
        string lName;
    }
    
    mapping (address => Instructor) instructors;
    address[] public instructorAccts;

function setInstructor(address _address, uint _age, string _fName, string _lName) public {
        var instructor = instructors[_address]; //ERROR HERE

        instructor.age = _age;
        instructor.fName = _fName;
        instructor.lName = _lName;
        
        instructorAccts.push(_address) -1;

    }

}

However, I am getting an error at the line var instructor = instructors[_address]

The error is ParserError: Expected primary expression

I am unable to understand what the issue is and how to resolve it. Could anyone help with this?

1

1 Answers

1
votes

Solidity uses typed variables, doesn't have the generic var keyword (that is used in JavaScript for example).


When declaring reference types (in your case string and struct), you need to specify their data location - in your case memory for the string argument. And either memory or storage for the struct depending on whether you want to set the values just in the context of the setInstructor() function (location memory), or if you want to set its values in the contract storage (location storage).

function setInstructor(address _address, uint _age, string memory _fName, string memory _lName) public {
     // either `memory` or `storage` depending on your use case
     Instructor storage instructor = instructors[_address];

     // ... rest of your code

There also is a syntax error on this line

instructorAccts.push(_address) -1;

you can fix it by removing the -1

instructorAccts.push(_address);