Since car.h uses truck, the include for truck.h must precede the include for car.h. Switch lines 5 and 6 in main.cpp.
how can I use type truck in car class and also type car in truck class
I see no reference to car in your truck class.
If you do indeed need to reference car in your truck class, you have what is called a circular dependency. The solution is to declare one as a forward.
You have implementations of the constructors within the definitions of the classes. That makes it harder to dodge dependencies. The class definition has to be in a header (with inclusion guards) so it can be included in multiple translation units, but the implementations should be in one source file.
> Since car.h uses truck, the include for truck.h must precede the include for car.h.
> Switch lines 5 and 6 in main.cpp.
.oi
put #include "truck.h" in car.h
As far as I can tell, your most recent code post is exactly the same as your first code post with the exception of the two forward declarations. Other than that you're not paying attention to what has been suggested.
1) You did not switch the order of includes (or put #include "truck.h" in car.h as suggested by ne555).
2) You have not implemented include guards as suggested by keskiverto) to prevent including the same file multiple times.
3) As I explained in my earlier port, something declared forward can NOT be passed by value as you are doing in line 19 of car.h. Change that to pass truck by reference. Note: You could pass by value here IF you had changed the order of the includes as I suggested earlier.
There is no special significance to the _ in an include guard. But, yes by convention an _ can be used to represent a . since . is not legal in a #define name. The point is the name being defined is unique to the file. i.e. We don't want to use #ifndef file_h in both car.h and truck.h.
The way include guards work is as follows:
1 2 3 4 5 6 7
// Some header file
#ifndef somehdr_h // If this file has been included before, then somehdr_h will be defined and everything up to the #endif will be skipped to prevent duplicate declarations.
// Everything within the ifndef will be included if this is the first inclusion
#define somehdr_h // Since this file has not been included before, we define some symbol that we can test to detect if the file has been included before.
// Declarations that should only be included once
#endif // Ends conditional inclusion
When something is passed by value, the compiler has to know how big it is (the object is pushed on the stack). When something is declared forward and the compiler has not seen the complete declaration yet, so the compiler does not yet know how big the object is. This is why you get an error such as "reference to incomplete type". i.e. The compiler knows the name exists, but not how bit it is.
Passing by reference or passing by pointer are okay, because the compiler only has to push a pointer onto the stack and the compiler knows how big pointers are.
Most of us are here to help beginners. However, there is an expectation that if you are given multiple suggestions on how to fix something that would follow at least some of the suggestions given.
This is where putting the body of the function into the header file fails.
Put the body into a source file, you allow yourself the ability to include the truck header, thus not making it an incomplete type, after the inclusion of the car header.
1 2 3 4 5 6 7 8 9 10 11 12
// car.cpp
#include "car.h"
#include "truck.h" // truck no longer an incomplete type
car::car(const truck& t)
: color(t.color)
{
// etc
}
Anyway, another solution would be to just have the truck and car derive from a similar class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
class Transportation
{
std::string color;
int length;
int width;
public:
void SetTransport(const Transportation& t) { *this = t; } // or something like that
};
class Truck : public Transportation
{
};
class Car : public Transportation
{
};
Why you would want to copy that kind of information into a car from a truck, doesn't really make sense in term of objects.
I'm sorry guys I tried much of your suggestions and I'm unable to work on my example please if you guys you have any other example/tutorial on circular dependency in C++ then please suggest me, now I want step by step followup.
Please a good circular dependency tutorial/example/video please
And I really appreciate your previous help, thanks for that.
You need the forward declaration still. The point of the forward declaration is for the header to know that Truck exists. That's why you were getting "incomplete type". Moving the body into a source file let's you include the details of the Truck so that you can now use the details of the Truck (no longer incomplete type).