Error: "classify 'class name' does not have a Companion Object,"

Asked

Viewed 884 times

2

My code

fun main(args: Array<String>) {

    val listaDeLikes = listOf(
        RelacaoDeLike(323423,1), //Usuario 323423 deu like no post 1
        RelacaoDeLike(234234,1), //Usuario 234234 deu like no post 1
        RelacaoDeLike(456456,1), //Usuario 456456 deu like no post 1
        RelacaoDeLike(233424,2), //Usuario 233424 deu like no post 2
        RelacaoDeLike(466656,2), //Usuario 466656 deu like no post 2
        RelacaoDeLike(536853,2), //Usuario 536853 deu like no post 2
        RelacaoDeLike(456456,2), //Usuario 456456 deu like no post 2
        RelacaoDeLike(464645,3), //Usuario 464645 deu like no post 3
        RelacaoDeLike(787423,3), //Usuario 787423 deu like no post 3
        RelacaoDeLike(685344,3), //Usuario 685344 deu like no post 3
        RelacaoDeLike(987987,3)  //Usuario 987987 deu like no post 3
    )

    val postId = 2     
    val totalDeLikes = contarNumeroDeLikes(postId, RelacaoDeLike)        
    println(">>> Aqui está o resultado: o post $postId tem um total de $totalDeLikes likes.")
}

fun contarNumeroDeLikes(postId: Int, listaDeLikes: List<RelacaoDeLike>): Int {    
    var totalDeLikes = 0       
    return totalDeLikes    
}


class RelacaoDeLike(userId: Int, postId: Int): Object() {
    val userId = userId
    val postId = postId

    init {           
        println("Foi criado uma nova relação de like entre o usuário $userId e o post $postId")
    }    
}

Error message:

source.kt:22:52: error: classifier 'Relacaodelike' does not have a Companion Object, and Thus must be initialized here val totalDeLikes = countNumerDeLikes(postid, Relacaodelike) ^

1 answer

6


It’s a typo.

This line

val totalDeLikes = contarNumeroDeLikes(postId, RelacaoDeLike)

Should be

val totalDeLikes = contarNumeroDeLikes(postId, listaDeLikes)

See working on Try.kotlinlang.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.