Need some help with a compiler issue
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Need some help with a compiler issue

  1. #1
    Join Date
    Mar 2011
    Posts
    15

    Did I compile this wrong?

    I was able to compile some code fine on ideone.com with only one import statement but when I try to compile the files on my local computer I get some errors. I'm not sure if I'm doing something wrong.

    Here is some more info:

    I copy and pasted the following two classes into the ideone.com web-based compiler and I didn't run into any problems upon compiling:

    Code:
    /****************************************
    *****************************************/
    /**
     * A class for numeric progressions.
     */
    
    import java.math.BigInteger;
    // public class Progression {  CHANGED IN ORDER TO COMPILE ON IDEONE.COM
    class Progression {
      /** First value of the progression.  */
      protected BigInteger first;
    
      /** Current value of the progression.  */
      protected BigInteger cur;
    
      /** Default constructor.  */
      Progression() {
        cur = first = BigInteger.ZERO;
      }
    
      /** Resets the progression to the first value.
       * 
       * @return first value
       */
      protected BigInteger firstValue() {
        cur = first;
        return cur;
      }
    
      /** Advances the progression to the next value.
       *
       * @return next value of the progression
       */
      protected BigInteger nextValue() {
        
        cur = cur.add(BigInteger.ONE);
        return cur; // default next value
      }
    
      /** Prints the first n values of the progression.
       * 
       * @param n number of values to print
       */
      public void printProgression(int n) {
        System.out.print(firstValue());
        for (int i = 2; i <= n; i++) 
          System.out.print(" " + nextValue());
        System.out.println(); // ends the line
      }
    
       
    }
    
    /****************************************
    *****************************************/
    /**
     * Arithmetic progression.
     */
    
    
    class ArithProgression extends Progression {
    
      /** Increment. */
      protected BigInteger inc;
    
      // Inherits variables first and cur.
    
      /** Default constructor setting a unit increment. */
      ArithProgression() {
        this(BigInteger.ONE);
      }
    
      /** Parametric constructor providing the increment. */
      ArithProgression(BigInteger increment) {
        inc = increment; 
      }
    
      /** Advances the progression by adding the increment to the current value.
       * 
       * @return next value of the progression
       */
      protected BigInteger nextValue() {
        cur = cur.add(inc);
        return cur;
      }
    
      //  Inherits methods firstValue() and printProgression(int).
    }
    However after I pasted the code into two java files on my local computer and tried to compile them:

    Progression.java
    Code:
    /**
     * A class for numeric progressions.
     */
    
    import java.math.BigInteger;
    // public class Progression {  CHANGED IN ORDER TO COMPILE ON IDEONE.COM
    class Progression {
      /** First value of the progression.  */
      protected BigInteger first;
    
      /** Current value of the progression.  */
      protected BigInteger cur;
    
      /** Default constructor.  */
      Progression() {
        cur = first = BigInteger.ZERO;
      }
    
      /** Resets the progression to the first value.
       * 
       * @return first value
       */
      protected BigInteger firstValue() {
        cur = first;
        return cur;
      }
    
      /** Advances the progression to the next value.
       *
       * @return next value of the progression
       */
      protected BigInteger nextValue() {
        
        cur = cur.add(BigInteger.ONE);
        return cur; // default next value
      }
    
      /** Prints the first n values of the progression.
       * 
       * @param n number of values to print
       */
      public void printProgression(int n) {
        System.out.print(firstValue());
        for (int i = 2; i <= n; i++) 
          System.out.print(" " + nextValue());
        System.out.println(); // ends the line
      }
    
       
    }
    ArithProgression.java
    Code:
    /**
     * Arithmetic progression.
     */
    
    
    class ArithProgression extends Progression {
    
      /** Increment. */
      protected BigInteger inc;
    
      // Inherits variables first and cur.
    
      /** Default constructor setting a unit increment. */
      ArithProgression() {
        this(BigInteger.ONE);
      }
    
      /** Parametric constructor providing the increment. */
      ArithProgression(BigInteger increment) {
        inc = increment; 
      }
    
      /** Advances the progression by adding the increment to the current value.
       * 
       * @return next value of the progression
       */
      protected BigInteger nextValue() {
        cur = cur.add(inc);
        return cur;
      }
    
      //  Inherits methods firstValue() and printProgression(int).
    }
    "javac Progression.java" compiles fine but "javac ArtihProgression.java" spits out the following problems:

    ArithProgression.java:13: cannot find symbol
    symbol : class BigInteger
    location: class ArithProgression
    protected BigInteger inc;
    ^
    ArithProgression.java:23: cannot find symbol
    symbol : class BigInteger
    location: class ArithProgression
    ArithProgression(BigInteger increment) {
    ^
    ArithProgression.java:31: cannot find symbol
    symbol : class BigInteger
    location: class ArithProgression
    protected BigInteger nextValue() {
    ^
    ArithProgression.java:19: cannot find symbol
    symbol : variable BigInteger
    location: class ArithProgression
    this(BigInteger.ONE);
    ^
    4 errors

    Am I compiling this wrong or do I have to put "import java.math.BigInteger;" into every file that extends "Progression.java" as well?
    Last edited by eindoofus; April 17th, 2011 at 12:16 PM.

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Need some help with a compiler issue

    You need to put the class import into every file that mentions the class.

    The file is a 'compilation unit' and import declarations apply within the scope of a compilation unit. See Import Declaration Scope and Import Declarations.

    If you understand what you're doing, you're not learning anything...
    Anon.
    Last edited by dlorde; April 17th, 2011 at 12:35 PM.
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center