枚举类中的错误

时间:2016-07-25 11:32:27

标签: java class enums

[ 44%] Built target opencv_features2d  
[ 44%] Linking CXX shared library ../../lib/libopencv_objdetect.so  
cd /tmp/opencv-20160725-28945-vyl9j5/opencv-2.4.13/macbuild/modules/objdetect && /home/usrname/.linuxbrew/Cellar/cmake/3.6.0/bin/cmake -E cmake_link_script CMakeFiles/opencv_objdetect.dir/link.txt --verbose=1  
/usr/bin/g++-5  -fPIC -Os -w -pipe -march=native    -fsigned-char -W -Wall -  Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-  security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -  Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-  template-args -Wno-array-bounds -Wno-aggressive-loop-optimizations -fdiagnostics-  show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -msse3 -  ffunction-sections -DNDEBUG  -DNDEBUG  -L/home/usrname/.linuxbrew/lib -Wl,--  dynamic-linker=/home/usrname/.linuxbrew/lib/ld.so -Wl,-  rpath,/home/usrname/.linuxbrew/lib -shared -Wl,-  soname,libopencv_objdetect.so.2.4 -o ../../lib/libopencv_objdetect.so.2.4.13   CMakeFiles/opencv_objdetect.dir/src/cascadedetect.cpp.o   CMakeFiles/opencv_objdetect.dir/src/datamatrix.cpp.o   CMakeFiles/opencv_objdetect.dir/src/distancetransform.cpp.o   CMakeFiles/opencv_objdetect.dir/src/featurepyramid.cpp.o   CMakeFiles/opencv_objdetect.dir/src/fft.cpp.o   CMakeFiles/opencv_objdetect.dir/src/haar.cpp.o   CMakeFiles/opencv_objdetect.dir/src/hog.cpp.o    CMakeFiles/opencv_objdetect.dir/src/latentsvm.cpp.o   CMakeFiles/opencv_objdetect.dir/src/latentsvmdetector.cpp.o   CMakeFiles/opencv_objdetect.dir/src/linemod.cpp.o   CMakeFiles/opencv_objdetect.dir/src/lsvmparser.cpp.o   CMakeFiles/opencv_objdetect.dir/src/lsvmtbbversion.cpp.o   CMakeFiles/opencv_objdetect.dir/src/matching.cpp.o    CMakeFiles/opencv_objdetect.dir/src/objdetect_init.cpp.o   CMakeFiles/opencv_objdetect.dir/src/resizeimg.cpp.o    CMakeFiles/opencv_objdetect.dir/src/routine.cpp.o   ../../lib/libopencv_highgui.so.2.4.13 -ldl -lm -lpthread -lrt   ../../lib/libopencv_imgproc.so.2.4.13 ../../lib/libopencv_core.so.2.4.13 -ldl -lm -lpthread -lrt -Wl,-rpath,/home/usrname/.linuxbrew/Cellar/opencv/2.4.13/lib   
cd /tmp/opencv-20160725-28945-vyl9j5/opencv-2.4.13/macbuild/modules/objdetect && /home/usrname/.linuxbrew/Cellar/cmake/3.6.0/bin/cmake -E cmake_symlink_library ../../lib/libopencv_objdetect.so.2.4.13 ../../lib/libopencv_objdetect.so.2.4   ../../lib/libopencv_objdetect.so  
make[2]: Leaving directory '/tmp/opencv-20160725-28945-vyl9j5/opencv-2.4.13/macbuild'   
[ 44%] Built target opencv_objdetect  
make[1]: Leaving directory '/tmp/opencv-20160725-28945-vyl9j5/opencv-2.4.13/macbuild'  
make: *** [Makefile:164: all] Error 2

READ THIS: https://github.com/Linuxbrew/brew/blob/master/share/doc/homebrew/Troubleshooting.md#troubleshooting
If reporting this issue please do so at (not Homebrew/brew):
  https://github.com/Homebrew/homebrew-science/issues

These open issues may also help:
public enum MaritalStatus
{
    Single, Divorcee, Married

}

public class Person
{
    protected int ID;
    protected String FirstName;
    protected String LastName;
    protected MaritalStatus Status;
    protected int Age;

    public Person(int id,String firstname,String lastname,MaritalStatus status,int age)
    {
        ID=id;
        FirstName=firstname;
        LastName=lastname;
        Status=status;
        Age=age;
    }
    public String toString()
    {
        return System.out.println("ID: "+ID + " First Name: "+FirstName+"   Last Name: " +LastName+"    Marital Status: "+ StringStatus +"  Age: "+Age);
    }
} 

2 个答案:

答案 0 :(得分:2)

枚举类"中没有"错误。您在toString中遇到问题(见下文)。您可能需要以下

public String toString()   {

  return  "ID: "+ID + " First Name: "+FirstName+"   Last Name: " +LastName+"    Marital Status: "+ Status +"    Age: "+Age;
}

问题:

  • 你不能返回System.out.println(…)(它返回无效,它不会"打印并返回字符串")
  • 要获取status的字符串版本,只需在此上下文中使用status(在字符串上使用+)或在其他上下文中使用status.toString()(其中{{1}期待的类型)。

其他(无关)问题

  • java中的fields / variables /参数通常以小写字母开头(Stringid等)
  • 字段通常为firstName而非private
  • 大多数人更喜欢运营商周围的空间,例如作业(protected
  • (恕我直言)尽可能使用a = b(例如finalval in other languages`)
  • 枚举字段通常采用大写字母(constSINGLE

以下是您班级的(更多)正确版本:

MARRIED

P.S。我猜这不是生产代码,因为有一个名为public class Person { private int id; private String firstName; private String lastName; private MaritalStatus status; private int age; public Person(final int id, final String firstName, final String lastName, final MaritalStatus status, final int age) { this.id = id; this.firstName = firstName; this.lastName = lastName; this.status = status; this.age = age; } @Override public String toString() { return "Person{" + "id=" + id + ", firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + ", status=" + status + ", age=" + age + '}'; } } 的字段有点尴尬

答案 1 :(得分:0)

我可以看到两个编译错误,还有一大堆样式错误。

这一行:

return System.out.println("ID: "+ID + " First Name: "+FirstName+
   "    Last Name: " +LastName+"    Marital Status: "+ StringStatus +
   "    Age: "+Age);

1)尚未声明变量StringStatus

1a)然后您将其更改为(String) Status,这也是错误的,因为您无法将MaritalStatus转换为String。 (但你可以打电话给toString() ....)

2)println方法是一种返回void而不是String的方法。

主要样式错误 1 是:

  • 字段名称不应以大写字母开头。
  • 您在令牌之前/之后使用空格是非标准且不一致的。在+=等中缀运算符之前和之后放置一个空格。 ,之前没有空格,之后没有空格。
  • 使用150+字符行是非标准的...并且使您的代码难以阅读。最多80个字符行最适合最大可读性。
  • 缩进8个空格是过分的。
  • 字段应为private而不是protected

    这不仅仅是风格。如果您没有将字段声明为private,则子类或(更糟糕的)其他一些不相关的代码可能会干扰字段的值。这意味着您必须阅读更多代码才能了解在跟踪错误时可能发生的情况(例如)。使用private有助于封装类型,从而使代码更易于阅读,更易于推理。

IMO,处理相同参数名称和字段名称的最佳方法(例如在构造函数中)是这样的:

public class Person {
    protected int id;

    public Person(int id) {
        this.id = id;   // Use 'this' to disambiguate the two meanings 
                        // of the 'id' identifier.
    }
}

1 - 不幸的是,一些“乐于助人”的人已经纠正了很多这样的问题,以使你的问题中的代码可读。请回头查看您最初发布的内容。