Incompatible types for returned object


 
Thread Tools Search this Thread
Top Forums Programming Incompatible types for returned object
# 1  
Old 02-23-2017
Incompatible types for returned object

I have a function from the Sedgewick's book
Code:
// return the shortest path from v to s as an Iterable
    public Iterable<String> pathTo(String v) {
        Stack<String> path = new Stack<String>();
        while (v != null && dist.contains(v)) {
            path.push(v);
            v = prev.get(v);
        }
        return path;
    }

When I compile the whole program, I always got error:
Code:
PathFinder.java:78: error: incompatible types
        return path;
               ^
  required: Iterable<String>
  found:    Stack<String>

The book website does not mention this bug. Quite vague with Iterable in Java so far. Can someone help me fix the problem? Thanks in advance.

Last edited by yifangt; 03-02-2017 at 05:47 PM..
# 2  
Old 03-02-2017
Quote:
Originally Posted by yifangt
I have a function from the Sedgewick'sbook
Code:
// return the shortest path from v to s as an Iterable
    public Iterable<String> pathTo(String v) {
        Stack<String> path = new Stack<String>();
        while (v != null && dist.contains(v)) {
            path.push(v);
            v = prev.get(v);
        }
        return path;
    }

When I compile the whole program, I always got error:
Code:
PathFinder.java:78: error: incompatible types
        return path;
               ^
  required: Iterable<String>
  found:    Stack<String>

The book website does not mention this bug. Quite vague with Iterable in Java so far. Can someone help me fix the problem? Thanks in advance.
It's not a bug. You will have to download and compile all the dependencies before compiling PathFinder.java - the dependencies are mentioned at the top blurb of each Java file. Also ensure the dependencies are in your CLASSPATH.

I tested the following using javac version 1.8 and 1.7 and they work fine with both.

Code:
Z:\data>
Z:\data>dir /b *
Graph.java
In.java
PathFinder.java
Queue.java
routes.txt
SET.java
ST.java
Stack.java
StdIn.java
StdOut.java
  
Z:\data>javac -version
javac 1.8.0_91
  
Z:\data>java -version
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)
  
Z:\data>javac StdOut.java
  
Z:\data>javac StdIn.java
  
Z:\data>javac In.java
  
Z:\data>javac SET.java
  
Z:\data>javac ST.java
  
Z:\data>javac Stack.java
  
Z:\data>javac Queue.java
  
Z:\data>javac Graph.java
  
Z:\data>javac PathFinder.java
  
Z:\data>type routes.txt
JFK MCO
ORD DEN
ORD HOU
ATL MCO
ORD HOU
DFW PHX
JFK ATL
ORD DFW
ORD PHX
ATL HOU
DEN PHX
PHX LAX
JFK ORD
DEN LAS
DFW HOU
ORD ATL
LAS LAX
  
Z:\data>java PathFinder routes.txt " " JFK
LAX
   JFK
   ORD
   PHX
   LAX
distance 3
MCO
   JFK
   MCO
distance 1
DFW
   JFK
   ORD
   DFW
distance 2
  
Z:\data>
Z:\data>

After cleaning up the .class files and setting up javac version 1.7:

Code:
Z:\data>
Z:\data>dir /b *
Graph.java
In.java
PathFinder.java
Queue.java
routes.txt
SET.java
ST.java
Stack.java
StdIn.java
StdOut.java
  
Z:\data>javac -version
javac 1.7.0_55
  
Z:\data>java -version
java version "1.7.0_55"
Java(TM) SE Runtime Environment (build 1.7.0_55-b13)
Java HotSpot(TM) Client VM (build 24.55-b03, mixed mode, sharing)
  
Z:\data>javac StdOut.java
 
Z:\data>javac StdIn.java
  
Z:\data>javac In.java
  
Z:\data>javac SET.java
  
Z:\data>javac ST.java
  
Z:\data>javac Stack.java
  
Z:\data>javac Queue.java
  
Z:\data>javac Graph.java
  
Z:\data>javac PathFinder.java
  
Z:\data>type routes.txt
JFK MCO
ORD DEN
ORD HOU
ATL MCO
ORD HOU
DFW PHX
JFK ATL
ORD DFW
ORD PHX
ATL HOU
DEN PHX
PHX LAX
JFK ORD
DEN LAS
DFW HOU
ORD ATL
LAS LAX
  
Z:\data>java PathFinder routes.txt " " JFK
LAX
   JFK
   ORD
   PHX
   LAX
distance 3
MCO
   JFK
   MCO
distance 1
DFW
   JFK
   ORD
   DFW
distance 2
  
Z:\data>
Z:\data>

Instead of downloading and compiling the dependencies individually, you could, alternatively, download their jar file and put it in your CLASSPATH.
This User Gave Thanks to durden_tyler For This Post:
# 3  
Old 03-02-2017
Thank you so much for pointing out the CLASSPATH issue, which indeed is the problem!
I upgraded my java to 1.8.0_121 and extracted their library to a sub-folder I forgot. According to you clue I edited CLASSPATH and it seems working now .....
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Programming

Incompatible data type fpos_t in C

This is from a program I wrote over in 1998 that I am trying to compile on a linux machine: void write_line (FILE *fp, int rec_no, line_rec *arec) { fpos_t woffset; woffset = (rec_no - 1) * sizeof(line_rec); fsetpos(fp,&woffset); fwrite(arec,sizeof(line_rec),1,fp); }On the line... (2 Replies)
Discussion started by: wbport
2 Replies

2. Programming

How to initialize an object with another object of different class?

How to initialize an object of class say "A", with an object of type say "B". The following code give the error message "error: conversion from âAâ to non-scalar type âBâ requested" #include <iostream> using namespace std; class B; class A{ public: A() { cout <<"\nA()" << endl; } ... (1 Reply)
Discussion started by: techmonk
1 Replies

3. Red Hat

Incompatible Level of gcc?

I'm compiling an application someone gave me. It uses XLC on a Power7, running Red Hat (4? 5?). It compiles and links, but I get the following message for every .o and .exe... xlc_r: 1501-274 (W) An incompatible level of gcc has been specified. I've tried googling on this error, and I'll I... (2 Replies)
Discussion started by: Harper21
2 Replies

4. Programming

GCC - Incompatible Pointer Types

Hi guys, here is my code written in C and the compiler error message. int i; int (*a); for (i = 1;i <= 9;i++) a = (int *)malloc(sizeof(int) * 10);here is the error: incompatible types when assigning to type ‘int’ from type ‘int *’I want to make a two dimensional array. I... (2 Replies)
Discussion started by: majid.merkava
2 Replies

5. UNIX for Dummies Questions & Answers

Object reference not set to an instance of an object

I am new to PHP and UNIX. I am using Apache to do my testing on a Windows Vista machine. I am getting this error when I am trying to connect to a web service. I did a search and did not see any posts that pertain to this. Here is my function: <?php function TRECSend($a, $b, $c, $d,... (0 Replies)
Discussion started by: EddiRae
0 Replies

6. UNIX for Advanced & Expert Users

UNIX patches with incompatible packages

I have a Sun Sparc machine with Solaris 9 on it as oracle server. We added two patches (112233-11: SunOS 5.9:Kernel Patch and 111722-04: SunOS 5.9:MathLibrary(libm)patch). When I prepared the server for Oracle installation, I checked patch with command: $/usr/sbin/patchadd -p | grep <patch_number>.... (0 Replies)
Discussion started by: duke0001
0 Replies

7. UNIX for Advanced & Expert Users

-pg incompatible with -shared

while compiling my code with -pg option i got the following error: ld (prelink): -pg incompatible with -shared; assuming -nopg any idea to overcome this problem? how can i use gprof profiler for a program using shared libraries? (2 Replies)
Discussion started by: yakari
2 Replies

8. UNIX for Advanced & Expert Users

rpc.rstatd: incompatible...

Hi, I'm working with Mercury tools: Loadrunner, and I'm trying to monitor a unix server by rstatd, and I got next error: Mar 4 11:25:56 sacindt rpc.rstatd: incompatible to /proc. Could not read disk_io: data does any one have an idea about this.. regards (0 Replies)
Discussion started by: toto2000ff
0 Replies
Login or Register to Ask a Question