Error - Going from Java to C++

This is a discussion on Error - Going from Java to C++ within the C++ Programming forums, part of the General Programming Boards category; I have created a linked list in Java, and now I am trying to convert my program to C++. When ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    9

    Error - Going from Java to C++

    I have created a linked list in Java, and now I am trying to convert my program to C++.
    When I try to compile, it gives me the error saying that 'next' uses 'CarNode' which is being defined. I am wondering if there is any way around this?

    Java:
    Code:
    public class CarNode {
      public Car carObject;
      public CarNode next;
    
      public CarNode (Car newCar, CarNode newNext) {
        carObject = newCar;
        next = newNext;
      }
    }

    C++:
    Code:
    class CarNode: public Car {
      public:
        Car carObject;
        CarNode next;
    
        CarNode(Car newCar, CarNode newNext) {
          carObject = newCar;
          next = newNext;
      }
    }

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    In C++ there are pointers (welcome to the Darkside)

    Code:
    class CarNode: public Car {
      public:
        Car *carObject;
        CarNode *next;
    
        CarNode(Car *newCar, CarNode *newNext) {
          carObject = newCar;
          next = newNext;
      }
    }
    Last edited by C_ntua; 11-06-2009 at 02:47 PM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    In C++ there is a class called list that you use if you need a list.

    What are you trying to accomplish?

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    #include <list>
    
    ...
    
    std::list<Car> cars;
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    Quote Originally Posted by C_ntua View Post
    In C++ there are pointers (welcome to the Darkside)
    In Java, everything is references. And references is just another name for pointers, because they behave exactly the same without the C syntax (which just makes them even more powerful).
    So I would say welcome back from the Dark Side. Java is Evil™. And their references are just plain stupid and pathetic.
    Welcome to C++, where everything does NOT have to be references or pointers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Java's references are all safe and colourful with flowers and sunshines. They are Good.
    C++ pointers are more powerful and more dangerous. They are Evil.

    Java is just more simplistic. Can agree on the pathetic, but not on the stupid.

    (To the point, they are the similar. So everywhere you have references in Java you will have to use pointers in C++. Somethings would be done differently probably in C++, so an exact translation is not the optimized solution)
    Last edited by C_ntua; 11-07-2009 at 04:58 AM.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    Quote Originally Posted by C_ntua View Post
    Java's references are all safe and colourful with flowers and sunshines. They are Good.
    C++ pointers are more powerful and more dangerous. They are Evil.
    I doubt that very much.
    They are not all colorful. Comparing two objects will compare their two references which is NOT what you would expect, for example. Is there a way to compare them, then? NO, because there is NO way to dereference the references. Instead, we must rely on C-ish ways of member functions for such stuff.

    C++'s pointers are a tool which complements other things in the language. The fact that you can point a pointer to an array and copy bytes to the beginning of a pointer are examples of what is dangerous, but that is not the default of the pointers, but other mechanics of the language.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Plus you still have NULL pointer exceptions in Java just like you do in C++, so I don't see how that's much safer. The only thing that's safer is that Java has garbage collection which reduces the chances of memory leaks.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,012
    First of all C++ classes end in ;.

    Second the code can also be written like this:
    Code:
    class CarNode: public Car {
      public:
        Car & carObject;
        CarNode &next;
    
        CarNode(Car &newCar, CarNode &newNext) {
          carObject = newCar;
          next = newNext;
      }
    };
    This makes it so that you cannot change what carObject and next point to, and that you cannot set them to NULL. Since this looks like a linked list implementation, this may not be what you want, but it's worth pointing out the alternative as it can be applied to other situations, and is generally prefered when it fits. Here you are using C++ references

    Third, this thead is horrible for explaining the differences between java pointers, and C++ pointers and references. You need a C++ book. In general though, C++ pointers are more powerful, because you can have pointers and references to primitive types. However, unlike java, you have to make sure that you don't try to dereference a NULL pointer, as it will lead to unpredictable results. (Java will just show you an error and close the program in a controlled mannor, or do whatever other beahior you specify).
    Last edited by King Mir; 11-07-2009 at 11:16 AM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mats, the java answers
    By Jaqui in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 04-22-2008, 02:12 AM
  2. C#, Java, C++
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 10-05-2004, 02:06 PM
  3. First Java Class at college
    By GanglyLamb in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 09-29-2004, 10:38 PM
  4. The Java language is being expanded
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 06-11-2004, 09:07 PM
  5. Java woes
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 07-06-2003, 12:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21