.:: Bots United ::.  
filebase forums discord server github wiki web
cubebot epodbot fritzbot gravebot grogbot hpbbot ivpbot jkbotti joebot
meanmod podbotmm racc rcbot realbot sandbot shrikebot soulfathermaps yapb

Go Back   .:: Bots United ::. > Developer's Farm > General Programming
General Programming Help others and get yourself helped here!

Reply
 
Thread Tools
liv(int) in Liv cannot be applied to () - Java
Old
  (#1)
Zacker
Project Leader, Lead Level Designer, Waypointer
 
Status: Offline
Posts: 337
Join Date: Mar 2004
Location: Denmark
Default liv(int) in Liv cannot be applied to () - Java - 04-06-2004

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.

Last edited by Zacker; 04-06-2004 at 22:38..
  
Reply With Quote
Re: liv(int) in Liv cannot be applied to () - Java
Old
  (#2)
stefanhendriks
RealBot Author
 
stefanhendriks's Avatar
 
Status: Offline
Posts: 3,088
Join Date: Nov 2003
Location: Netherlands
Default Re: liv(int) in Liv cannot be applied to () - Java - 04-06-2004

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.


Author of RealBot, "Arrakis" and "Dune 2 - The Maker" | co-Founder of Bots-United | Fundynamic | Blog | E-Mail me

Last edited by stefanhendriks; 04-06-2004 at 22:54..
  
Reply With Quote
Re: liv(int) in Liv cannot be applied to () - Java
Old
  (#3)
KickBot
Member
 
Status: Offline
Posts: 17
Join Date: Apr 2004
Default Re: liv(int) in Liv cannot be applied to () - Java - 05-06-2004

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.

Last edited by KickBot; 05-06-2004 at 12:50..
  
Reply With Quote
Re: liv(int) in Liv cannot be applied to () - Java
Old
  (#4)
Zacker
Project Leader, Lead Level Designer, Waypointer
 
Status: Offline
Posts: 337
Join Date: Mar 2004
Location: Denmark
Default Re: liv(int) in Liv cannot be applied to () - Java - 05-06-2004

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.
  
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
vBulletin Skin developed by: vBStyles.com