声明错误:未找到标识符或标识符不唯一。映射(uint => product)公共产品;

时间:2021-06-28 08:00:45

标签: ethereum solidity smartcontracts

下面是我的代码,我不知道为什么给我这个错误。

// SPDX-License-Identifier: MIT
pragma solidity ^0.5.16;

// creating the contract
contract Rating {
    
    // creating structure to model the product
    struct Product {
        uint id;
        string name;
        uint RatingCount;
    }

    // use mapping to get or fetch the contestant details
    mapping(uint => product) public products;

    // add a public state variable to keep track of product count
    uint public productsCount;

    constructor () public {
        addProduct("Nike");
        addProduct("Adidas");
    }

    // add a function to add product
    // for private variable we use underscore in the start of variable _name
    function addProduct(string memory _name) private {
        productsCount++;
        products[productsCount] = Product(productsCount, _name, 0);
    }

}

2 个答案:

答案 0 :(得分:0)

Solidity 区分大小写。

您已经定义了一个名为 Product(大写 P)的结构,但您的映射使用的是 product(小写 p)。

解决方案:使用正确的形式Product

mapping(uint => Product) public products;

答案 1 :(得分:0)

抱歉,我发现问题出在映射中

mapping(uint => product) public products;

我错误地使用了小 p 而不是大写 P。这在迁移我的智能合约时给了我一个错误,因为我的结构名称以大写 P(产品)开头。 所以不是小 p 只是将 p 更改为 P。

mapping(uint => Product) public products;
相关问题