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:

public class CarNode {
  public Car carObject;
  public CarNode next;

  public CarNode (Car newCar, CarNode newNext) {
    carObject = newCar;
    next = newNext;
  }
}

C++:

class CarNode: public Car {
  public:
    Car carObject;
    CarNode next;

    CarNode(Car newCar, CarNode newNext) {
      carObject = newCar;
      next = newNext;
  }
}

I am a little doubtful about your code. Why does CarNode inherit from
Car and it also composes it ?

Note that in c++ objects is by default passed by value and Not passed by
reference. You may wan't to read about pointers and reference in c++.

Yeah, I don't see the word "extends" in your Java code, so I'm doubtful of the inheritance. firstPerson is right regarding pointers and pass-by-value. I'd have to see the overall set-up, but you're probably looking for something ike:

class CarNode/*: public Car */
{
  public:
    Car* carObject;
    CarNode* next;

    CarNode(Car* newCar, CarNode* newNext) 
    {
      carObject = newCar;
      next = newNext;
  }
};

Note that I commented the inheritance out. Also, both in C++ and Java, consider, making the data members private and using getter and setter methods.

This article has been dead for over six months. Start a new discussion instead.