|
-
November 6th, 2009, 04:30 PM
#1
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:
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;
}
}
-
November 6th, 2009, 04:34 PM
#2
Re: Error - Going from Java to C++
A "manual" linked list implementation in C++ must use pointers. A CarNode can't contain another CarNode directly, because that would imply sizeof(CarNode) > sizeof(CarNode), which is a contradiction. However, a CarNode can easily contain a pointer to another CarNode.
In Java, "everything is pointers," so the concept shouldn't be much trouble, just the syntax.
If this isn't for a class, though, I'd suggest saving yourself some trouble and just using a std::list<Car>, which is more or less the equivalent of Java's LinkedList class.
Last edited by Lindley; November 6th, 2009 at 04:37 PM.
-
November 6th, 2009, 04:46 PM
#3
Re: Error - Going from Java to C++
 Originally Posted by icu222much
I have created a linked list in Java, and now I am trying to convert my program to C++.
Doesn't Java already have a linked list class? C++ already has std::list, so I don't know why you need to create such a class.
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 is not C++, and C++ is not Java. Just because the languages look the same doesn't mean that you can code them the same way.
The proper way to learn C++ in this case is to pretend Java does not even exist. If you try to code C++ as if you're coding Java, you will create more trouble for yourself.
For example:
Code:
CarNode(Car newCar, CarNode newNext) {
You are passing objects by value here. In C++, you should not pass these objects by value -- these parameters should be passed by reference or const reference, not by value. There is a huge difference between passing by value and by reference, but if you just copy what Java does, you never know the difference.
This is just one of the things that will get you in trouble if you write code in C++ trying to copy what Java syntax does.
Regards,
Paul McKenzie
-
November 7th, 2009, 04:40 AM
#4
Re: Error - Going from Java to C++
At the point where you are referring to CarNode as a member object CarNode is not fully defined (because you are still defining it). At that instant, CarNode is still incompletely defined. It has just been named in the scope where you declare the member object. And you can only refer to an incomplete type i.e. either have a pointer to a reference. In Java, you can get away with that because the same syntax there would mean declaring a reference member and not actually an object as a member. So, in C++ you can't have something like this (which is what you are essentially doing):rather you can have this though:
Code:
class A
{
A& a;
//or
//A* a; that is, you can have a pointer or reference to an incomplete type
};
This what the standard says: [3.1/5] basic.def
Code:
A program is ill-formed if the definition of any object gives the object an incomplete type
Another ocassion where this might happen is if you had a forward declaration of a class and you tried to do the same as above.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|