发现不兼容的类型:无效,有什么问题?

时间:2012-10-16 03:28:35

标签: java types vector 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);
  }

2 个答案:

答案 0 :(得分:4)

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

由此,成员函数add不会返回任何可以放入result的内容。使用如下行:

x = x + other.x;

(其中x是“当前对象”的成员,而other是您要添加到其中的对象),one.Add (two)意味着<{1}} em>修改 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);

答案 1 :(得分:0)

根据您的方法声明public void add(Vec2 other),您要将two添加到one。因此 one本身就是您的结果,因此无需返回。

只需删除return语句并将one视为结果对象。