发现不兼容的类型:void,有什么问题? [英] incompatible types found : void, what is wrong?

查看:34
本文介绍了发现不兼容的类型:void,有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个查找最接近的两个向量并返回总和的类.

I am trying to write a class that find the closest two vectors and return a sum.

我一直在努力理解,但我找不到收到此消息的原因,这是我收到的唯一错误:

I have tried to understand so hard but I can't find the reason why I get this message, it's the only error I get:

java:93:不兼容的类型发现:无效需要:EDU.gatech.cc.is.util.Vec2结果 = 一.加(二);^

java:93: incompatible types found : void required: EDU.gatech.cc.is.util.Vec2 result = one.add(two); ^

第93行在代码的最后,我放了一些箭头来表示!

Line 93 is at the end of the code, I put some arrows to indicate it!

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);
  }

推荐答案

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

由此,成员函数add 不会返回任何可以放入result 的内容.像这样的一行:

From this, the member function add does not return anything that you can put into result. With a line like:

x = x + other.x;

(其中 x 是当前对象"的成员,other 是您要添加到它的对象), 是绝对确定的one.Add (two) 是为了修改 one 而不是在计算中使用它.

(where x is a member of "the current object" and other is the object you're adding to it), it's a dead certainty that one.Add (two) is meant to modify one rather than just use it in a calculation.

所以,而不是:

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

你可能需要这样的东西:

you'll probably need something like:

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

这篇关于发现不兼容的类型:void,有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆