2012-10-16 53 views
1

我想寫一個類,找到最接近的兩個向量並返回一個總和。找到不兼容的類型:void,出錯了?

我試圖弄明白這麼辛苦,但我找不到爲什麼我得到這個消息的原因,它是唯一的錯誤,我得到:

的java:93:不兼容的類型 發現:無效 要求:EDU .gatech.cc.is.util.Vec2 result = one.add(two); ^

93行是在代碼的末尾,我放了一些箭頭來表示它!

enter code here 


package EDU.gatech.cc.is.clay; 


import java.util.*; 
import EDU.gatech.cc.is.clay.*; 
import java.lang.*; 
import EDU.gatech.cc.is.abstractrobot.*; 
import EDU.gatech.cc.is.util.Vec2; 
import EDU.gatech.cc.is.util.Units; 


public class MAX_go_in_between extends NodeVec2 
{ 
    public static final boolean DEBUG = /*true;*/ Node.DEBUG; 
    private SocSmall abstract_robot; 


    public MAX_go_in_between(SocSmall ar) 
    { 
    abstract_robot = ar; 


    } 

    long last_spott = 0; 
    Vec2 result = new Vec2(); 



    public Vec2 Value(long timestamp) 
    { 


    if (DEBUG) System.out.println("MAX_Avoid_walls: Value()"); 

    if ((timestamp > last_spott) || (timestamp == -1)) 
    { 
     if (timestamp != -1) last_spott = timestamp; 


     Vec2 one; 
     Vec2 two; 

     //array of Vec2 of all the opponents 
     Vec2[] list_opp = abstract_robot.getOpponents(timestamp); 
     //empty array of vec2 where will be put the opponents in front of the robot 
     ArrayList<Vec2> list_opp_in_front; 

     Vec2 temp; 


     // find which opponents are in front and put them in the arraylist 
     for(int i=0; i<list_opp.length; i++) 
     { 
     temp = list_opp[i]; 

     if(temp.x >= 0.0) 
     { 
      list_opp_in_front.add(temp); 
     } 
     } 

     //get closest opponent and sets it to index 0 
     for(int i=1; i<list_opp_in_front.size()-1; i++) 
     { 
     temp = list_opp_in_front.get(i); 

      if(list_opp_in_front.get(0).r<temp.r) 
     { 
      list_opp_in_front.set(i, list_opp_in_front.get(0)); 
      list_opp_in_front.set(0, temp); 

     } 
     } 

     //get second closest opponent and sets it to index 1 
     for(int i=2; i<list_opp_in_front.size()-1; i++) 
     { 
     temp = list_opp_in_front.get(i); 

      if(list_opp_in_front.get(1).r<temp.r) 
     { 
      list_opp_in_front.set(i, list_opp_in_front.get(1)); 
      list_opp_in_front.set(1, temp); 
     } 

      // sum both vectors 
      one = list_opp_in_front.get(0); 
      two = list_opp_in_front.get(1); 

=============>>>> 
=============>>>> result = one.add(two); 
      } 

     } 

     return(result); 
    } 

    } 



Here is the Vec2.add(Vec2) method: 


public void add(Vec2 other) 
    { 
    x = x + other.x; 
    y = y + other.y; 
    r = Math.sqrt(x*x + y*y); 
    if (r > 0) 
    t = Math.atan2(y,x); 
    } 
+3

如何指出哪一行是93行。另外,什麼是方法'Vec2.add(Vec2)'的聲明? –

+0

請分享'Vec2'的'add'方法聲明。 –

回答

3
result = one.add (two); 
public void add (Vec2 other) 
//  ^^^^ 

由此看來,成員函數add不返回任何東西,你可以投入result。有了這樣一行:

x = x + other.x; 

(其中x是「當前對象」和other的成員是您要添加到它的對象),這是一個死確定性one.Add (two)意味着修改one而不僅僅是它來計算。

因此,而不是:

one = list_opp_in_front.get (0); 
two = list_opp_in_front.get (1); 
result = one.add (two); 

你可能會需要這樣的東西:

result = list_opp_in_front.get (0); 
two = list_opp_in_front.get (1); 
result.add (two); 
+0

好的,我已經用箭頭表示93行,並添加了Vec2.add(Vec2), 感謝您的快速回答!我不習慣在這裏提問,對此抱歉! – user1748822

+0

好的,謝謝,我覺得我很笨...... – user1748822

0

按照您的方法聲明public void add(Vec2 other),要添加到twoone。因此one本身就是你的結果,因此不需要返回。

只是刪除return語句並將one作爲結果對象。

+0

@負用戶:請留下一些評論,以幫助理解答案的錯誤? –

相關問題