1
pragma solidity ^0.5.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 memory _fName, string memory _lName) public {
    var instructor = instructors[_address];

    instructor.age = _age;
    instructor.fName = _fName;
    instructor.lName = _lName;

    instructorAccts.push(_address) -1;
}

function getInstructors() public view returns ( address[]) {
    return instructorAccts;
}
}

I keep getting errors saying the var keyword is disabled and data location must be memory for return parameter in function, but none was given.

0

I think the compiler errors are fairly self-explanatory, but here's a fixed version:

pragma solidity ^0.5.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 memory _fName, string memory _lName) public {
        Instructor storage instructor = instructors[_address];

        instructor.age = _age;
        instructor.fName = _fName;
        instructor.lName = _lName;

        instructorAccts.push(_address);
    }

    function getInstructors() public view returns (address[] memory) {
        return instructorAccts;
    }
}
  • I gave a proper type to instructor.
  • I gave a data location for instructor.
  • I gave the return value of getInstructors a data location.
  • I deleted the strange -1 that wasn't doing anything.
  • Thank you I am trying to learn about data structures and mapping. But for some reason when I run this in remix I don't get any of the names or ages I listed. I just get the address. – user3393561 Jun 12 at 0:22
  • Oops, I was confused between instructors and instructorAccts. Edited my answer to reflect that. getInstructors just returns an array of addresses, so yes, it's expected that you'll only get addresses back. If you want to make a function that returns something else, you can certainly do that. – smarx Jun 12 at 0:27
  • thank you! I'ma play around with this code and see if I can update it. It's from 2017 apparently. – user3393561 Jun 12 at 0:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.