944,167 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 458
  • C++ RSS
Nov 6th, 2009
0

Error - Going from Java to C++

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  1. public class CarNode {
  2. public Car carObject;
  3. public CarNode next;
  4.  
  5. public CarNode (Car newCar, CarNode newNext) {
  6. carObject = newCar;
  7. next = newNext;
  8. }
  9. }


C++:
C++ Syntax (Toggle Plain Text)
  1. class CarNode: public Car {
  2. public:
  3. Car carObject;
  4. CarNode next;
  5.  
  6. CarNode(Car newCar, CarNode newNext) {
  7. carObject = newCar;
  8. next = newNext;
  9. }
  10. }
Reputation Points: 10
Solved Threads: 0
Light Poster
icu222much is offline Offline
26 posts
since Feb 2009
Nov 6th, 2009
0
Re: Error - Going from Java to C++
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++.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,865 posts
since Dec 2008
Nov 6th, 2009
0
Re: Error - Going from Java to C++
thank you.
Reputation Points: 10
Solved Threads: 0
Light Poster
icu222much is offline Offline
26 posts
since Feb 2009
Nov 7th, 2009
0
Re: Error - Going from Java to 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:

C++ Syntax (Toggle Plain Text)
  1. class CarNode/*: public Car */
  2. {
  3. public:
  4. Car* carObject;
  5. CarNode* next;
  6.  
  7. CarNode(Car* newCar, CarNode* newNext)
  8. {
  9. carObject = newCar;
  10. next = newNext;
  11. }
  12. };

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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Recursion Help!
Next Thread in C++ Forum Timeline: general protection error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC