kotlincompiler-errorssyntax-error

"Expecting member declaration"


I'm having trouble with these lines of code:

class MainActivity : ComponentActivity() {
    var item = "None"
    var price: Double = 16.99
    var Quantity: Int = 2
    println(Calculate(price, item, Quantity)) 
}
fun Calculate(price: Double, item: String, Quantity: Int): Double {
    return(Quantity*price)
}

Solution

  • You can't have statements directly under a class, they should be inside a constructor or a method. E.g.:

    class MainActivity  {
        var item = "None"
        var price: Double = 16.99
        var Quantity: Int = 2
        fun someFunc() {
            println(Calculate(price, item, Quantity)) 
        }
    }