.:: Bots United ::.

.:: Bots United ::. (http://forums.bots-united.com/index.php)
-   General Programming (http://forums.bots-united.com/forumdisplay.php?f=25)
-   -   liv(int) in Liv cannot be applied to () - Java (http://forums.bots-united.com/showthread.php?t=1876)

Zacker 04-06-2004 21:21

liv(int) in Liv cannot be applied to () - Java
 
Compile error:
Code:

liv(int) in Liv cannot be applied to ()
    stats_sum = styrke + intelligens + liv.liv()/10;

With a ^ below the dot.

I initialize the method/function here:
Code:

//Opret et objekt af klassen Liv 
Liv liv = new Liv();

The method/function:
Code:

//Justerer eller returnerer spillerens liv
        public int liv(int a)
        {
          if(a!=0)
          {
          //Giver spilleren a mere i liv, hvis a ikke er 0
          liv = liv + a;
      System.out.println("Du har nu " + liv + " i liv");
          }
          else
          {
            //Returnerer værdien af liv til den kaldende funktion
            return liv;
          }

  }

I hope it obvious that I just want the value of liv:)

Edit:
It works if I call it with the parameter 0. Then I thought it would help initializing the variable in the method, like this:
public int liv(int a=0)
That just resulted in a bunch of compile errors.

stefanhendriks 04-06-2004 21:49

Re: liv(int) in Liv cannot be applied to () - Java
 
its logical that you have to pass something to liv(), since you ask for ONE parameter. This would not compile in C either ;)

i think this:

Code:

stats_sum = styrke + intelligens + liv.liv()/10;
should be:
Code:

stats_sum = styrke + intelligens + liv.liv/10;



since you want to know the liv as variable, not the function. I dont see any input so i guess you want the liv number... If you want to use the function you should probably do:

Code:

stats_sum = styrke + intelligens + liv.liv(0)/10;
which does the same as the previous line i gave, since it just returns 'liv' as variable.

KickBot 05-06-2004 11:46

Re: liv(int) in Liv cannot be applied to () - Java
 
stefan has the answer.

As a more general remark, you shouldn't give the same name to a method and a member variable. This will only cause you more problems like this one in the future. Java coders prefer stuff like getXXX, setXXX, addXXX etc...

Also Java and C++ look similar in some places but there are differences, like no default parameters.
In C++ you would write:
Code:

void foo( int i = 0 ) { ... }
In Java you can't, that's why the compiler refuse to compile your code, and the Java way is to do:
Code:

void foo() { foo(0); }
void foo( int i ) { ... }

Well. At least for j2 1.4, I've heard 1.5 has syntax changes and new stuff.

Hope this heps.

Zacker 05-06-2004 14:47

Re: liv(int) in Liv cannot be applied to () - Java
 
Quote:

Java coders prefer stuff like getXXX, setXXX, addXXX etc...
That was also how I did it first time. Then I thought it would be smarter just to combine the functions.


All times are GMT +2. The time now is 12:42.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.