I have a mapping of a struct, which has a mapping to another struct:
uint256 public questionIdCounter;
mapping(uint256 => Question) public questions;
struct Question {
uint256 answerCounter;
mapping(uint256 => Answer) answers;
}
struct Answer {
address answerer;
}
And I'm looking to add a new Question
, which can have Answer
s added to it.
function askQuestion() public {
questions[questionIdCounter] = Question();
questionIdCounter = questionIdCounter + 1;
}
And then people can add answers
function answerQuestion(uint256 questionId) public {
// Add new answer
questions[questionId].answers[questions[questionId].answerCounter] = Answer({answerer: msg.sender});
// update answer counter
questions[questionId].answerCounter = questions[questionId].answerCounter + 1;
}
However, when I look at kbase_questions.questions(0)
I only get to see the answerCounter
and not the answer
mapping.
Why is my empty mapping not being initialized?
I've looked at How to initialized struct with member variable type mapping and How to initialized struct with member variable type mapping, which seem to be the closest to what I'm looking to do, but they are declaring their struct outside the function, which I can't do because I have many structs.
askQuestion()
function in0.7.0
, because of the struct constructorQuestion()
. You might have to redesign the structs so that they don't contain a mapping inside.