Search code examples
ethereumsoliditysmartcontracts

Member "team1Score" not found or not visible after argument-dependent lookup in type (contract Game) - Solidity


I'm on a course and have run into a problem.

I am trying to make a function that shows the difference in team score from the perspective of the team in the variable teamNumber.

My issue is that when trying to import the variable "team1Score" or "team2Score" from the other contract Game.sol I get the error - Member "team1Score" not found or not visible after argument-dependent lookup in type (contract Game)

Here is the Game.sol contract:

pragma solidity ^0.8.4;

contract Game {
    int public team1Score;
    int public team2Score;

    enum Teams { Team1, Team2 }

    function addScore(Teams teamNumber) external {
        if (teamNumber == Teams.Team1) {
            team1Score +=1;
        } else if (teamNumber == Teams.Team2) {
            team2Score +=1;
        }
    }
}

Here is the code for Bet.sol which references Game.sol. The problem lies within the function "getScoreDifference"

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

import "./Game.sol";

contract Bet {
    address public game;



    constructor (address gameContract) {
        game = gameContract;
    }
     
    // calculates the payout of a bet based on the score difference between the two teams
    function calculatePayout(uint amount, int scoreDifference) private pure returns(uint) {
        uint abs = uint(scoreDifference > 0 ? scoreDifference : scoreDifference * -1);  
        uint odds = 2 ** abs;
        if(scoreDifference < 0) {
            return amount + amount / odds;
        }
        return amount + amount * odds;
    }

    function getScoreDifference (Game.Teams x) public view returns (int256){
        if (x == Game.Teams.Team1) {
            return Game.team1Score - Game.team2Score;
        } else if (x == Game.Teams.Team2) {
            return Game.team2Score - Game.team1Score;
        }
    }
}


Solution

  • this

    constructor (address gameContract) {
        game = gameContract;
    }
    

    should be

    constructor (address gameContract) {
        game = Game(gameContract);
    }
    

    and

    function getScoreDifference (Game.Teams x) public view returns (int256){
        if (x == Game.Teams.Team1) {
            return Game.team1Score - Game.team2Score;
        } else if (x == Game.Teams.Team2) {
            return Game.team2Score - Game.team1Score;
        }
    }
    

    should be

    function getScoreDifference (Game.Teams x) public view returns (int256){
            if (x == Game.Teams.Team1) {
                return game.team1Score - game.team2Score;
            } else if (x == Game.Teams.Team2) {
                return game.team2Score - game.team1Score;
            }
        }