MorGa
MorGa

Reputation: 23

Solidity change data location from memory to storage

Hi I have this code and the error is :

UnimplementedFeatureError: Copying of type struct spu_university.all_courses memory[] memory to storage not yet supported.

How I can solve this ??

// SPDX-License-Identifier: MIT pragma solidity >=0.4.22 <0.9.0;

contract spu_university {

address  is_admin;
constructor(){
    is_admin = msg.sender;
}

modifier onlyAdmin(address x) {
    require(x== is_admin, "Permission Denied. You do not have admin access.");
    _;  }

uint s_counter=0;

struct students_marks{
    uint id;
    uint mark;
}

struct  all_courses{

    string course_name ;
    uint mark ;
}

struct warning {

    string date ;
    string description;
}

struct  course {
    string name ;
    uint id;
    uint weekly_hours;
    uint level ;

    
    students_marks[] marks;
    }

    struct prof {
    
    uint id ;
    string firstName;
    string lastName;
    string fatherName;
    string motherName;
    string gender;
    string email;
    string collegeName;
    string dateOfBirth;
    string mobileNumber; 
    string specialization ;


}

struct  student  {

    uint  id ;
    string firstName;
    string lastName;
    string fatherName;
    string motherName;
    string gender;
    string email;
    string collegeName;
    string dateOfBirth;
    string mobileNumber;
    
    all_courses[] course;
    
    warning[] s_warnings;
    
    }

student[] studentList ;
prof[] profList;
course[] courselist; 

mapping(uint => student) public getstudent;
mapping(uint => prof) public getprof;   
mapping(uint => course) public getcours; 

//---------------------------------- registrations functions -------------------------------------------------------

function registerStudent(
    uint  _id,
    string memory _firstName,
    string memory _lastName,
    string memory _fatherName,
    string memory _motherName,
    string memory _gender,
    string memory _email,
    string memory _collegeName,
    string memory _dateOfBirth,
    string memory _mobileNumber

    ) onlyAdmin(msg.sender) public {

    
          student memory x= student(_id,_firstName,
          _lastName,_fatherName,_motherName,_gender,
          _email,_collegeName,_dateOfBirth,
          _mobileNumber,
          new all_courses[] (0),
          new warning[] (0) );
          
          studentList.push(x);

    getstudent[_id]=studentList[s_counter];
    s_counter ++;
    
}

}

I tried a lot of things did not work.

Upvotes: 2

Views: 229

Answers (1)

norym
norym

Reputation: 707

You have a storage - memory mismatch here. I wont go into detail what storage and memory is about, you can read about it here here.

Also, you find other stackoverflow posts about similar problems to yours which explain why this specific error occurs, for example see here and here.

However, I will give you some starting point on how to restructure your code, so you do not run into the same error again. Have a look at the following more simplified example derived from yours.

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;


contract spu_university {
    constructor(){
    }

    uint s_counter=0;

    struct course {
        string name;
    }

    struct student {
        uint  id;
        string firstName;
        course[] all_courses;
    }


    mapping(uint => student) public getstudent;
    
    function registerStudent(
        uint  _id,
        string memory _firstName
        ) public {
            student storage s = getstudent[_id];

            s.id = _id;
            s.firstName = _firstName;

            course memory c;
            c.name = "some course name";
            s.all_courses.push(c);

            s_counter ++; 
    }

    function addCourseToStudent(uint  _id) public {
        student storage s = getstudent[_id];

        course memory c;
        c.name = "some other course name";

        s.all_courses.push(c);
    }

    function getCourseOfStudent(uint _id) public view returns (course[] memory) {
        return getstudent[_id].all_courses;
    }
}

Upvotes: 1

Related Questions