1
// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

contract PokemonFactory {

struct Pokemon {
  uint id;
  string name;
}

struct Type {
  string name;
  string description;
}

struct Habilities {
  string name;
  string description;
}

struct Weaknesses {
  string name;
  string description;
}

Pokemon[] private pokemons;
Habilities[] private hability;
Type[] private pokemontype;
Weaknesses[] private weaknesses;

mapping (uint => Type) public pokemonType;
mapping (uint => address) public pokemonToOwner;
mapping (address => uint) ownerPokemonCount;

event eventNewPokemon(string message, string name, uint indexed id);

constructor () public {
  string[5] data; 
  // Expected '(' but got identifier solidity(2314)
  function typepo () public returns (string[5] memory) {
    data = ["1", "2", "3", "4", "5"];
    return data;
  }
}

function createPokemon (string memory _name, uint _id) public {
  require(_id > 0, "El id debe ser mayor a 0");
  require(bytes(_name).length > 2, "El nombre no debe estar vacio y ser mayor a 2 caracteres");

  pokemons.push(Pokemon(_id, _name));
  pokemonToOwner[_id] = msg.sender;
  ownerPokemonCount[msg.sender]++;
    
  emit eventNewPokemon("Nuevo Pokemon Creado", _name, _id);
}

function getAllPokemons() public view returns (Pokemon[] memory) {
  return pokemons;
}

function getResult() public pure returns(uint product, uint sum){
  uint a = 1; 
  uint b = 2;
  product = a * b;
  sum = a + b; 

}

}

Any idea?

2 Answers 2

0

The mistake in the code is that you are trying to declare a function inside the constructor. This is not allowed in Solidity, and it will result in a compilation error. Functions can not be declared inside another function or constructor.

Here's an example of how you can fix the code to declare the function outside the constructor:

string[5] public data;

constructor () {
    data = ["1", "2", "3", "4", "5"];
}

function typepo () public view returns (string[5] memory) {
    return data;
}

Hope it helps!

0

So you accidentally try to create a function named typepo() inside the constructor, which will not work. Just get that function out of the constructor block and it will work fine.

Also, you need to format the code a little bit. The data variable needs to be outside the constructor and just the initialization needs to be in the constructor. Try something like this:

   string[5] data;

    constructor() {
        data = ["1", "2", "3", "4", "5"];
    }

    function typepo() public view returns (string[5] memory) {
        return data;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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