using two differenct classes

Pages: 12
... you can't pass a forward reference by value.

Herb Sutter disagrees with you. See the link from my first post.

Forward declarations should be enough.
Seems like I'm much close brothers now I changed the code just one problem left now

The code below gets compiled fine unless I out comment line 13 in truck.cpp or in other words unless I start using received reference to car type object in truck class

So far what I've done is

1. created two classes say C & T
2. declared reference to T in C
3. used reference to T in C
4. declared reference to C in T
5. COULD NOT USE REFERENCE TO "C" IN "T" (as soon as I use reference to C in T COMPILER ERROR's like use of incomplete type)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//main.cpp

#include <iostream>

using namespace std;

#include "truck.h"
#include "car.h"

int main(){
	
	cout << "---------Program Initiated-----------" << endl << endl;
	
	car cobj;
	truck * ptrtruck = new truck(cobj);
	
	truck tobj;
	car * ptrcar = new car(tobj);
	
	delete ptrtruck;
	delete ptrcar;
	
	return 0;
	
}//---------main() 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//car.h

#ifndef car_h
#define car_h

class car{
	
	public:
		
		std::string type;
		int year;
		
		car();
		
		car(truck &);
		
		void print();
	
};

#include "car.cpp"

#endif 


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

car::car(){
	
	type = "Car";
	year = 2014;
	
}//------constructor


car::car(truck & tobj){
	
	type = "Car";
	year = tobj.year;
	
}//------------constructor(truck)

void car::print(){
		
		std::cout << "Type: " << type << "\tYear: " << year << endl;
		
	}//---------print() 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//truck.h

#ifndef truck_h
#define truck_h

class car;

class truck{
	
	public:
		
		std::string type;
		int year;
		
		truck();
		
		truck(car &);
		
		void print();
	
};

#include "truck.cpp"

#endif 


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

truck::truck(){
	
	type = "Truck";
	year = 2012;
	
}//------constructor()

truck::truck(car & cobj){
		
		type = "Truck";
//		year = cobj.year;
		
}//--------constructor(car)

void truck::print(){
	
	std::cout << "Type: " << type << "\tYear: " << year << endl;
	
}//----------print() 
closed account (10X9216C)
Still missing the Truck forward declaration in car.h.

> ... you can't pass a forward reference by value.

Herb Sutter disagrees with you. See the link from my first post.

Forward declarations should be enough.


You can't by value, that's what generates the incomplete type error. You can by reference or pointer.
Last edited on
myesolar I don't get, I mean

1. in main.cpp line 7 I have included class truck before class car so now compiler before reaching class car know the whole definition of class truck then why should now class car need forward declaration of class truck?

2. Also in truck.cpp line 10 I'm passing car type object by reference not by value and error will be generated when I use this referred object
You can't by value, that's what generates the incomplete type error.

Yes I can, and GCC 4.4.7 was totally happy about it too.
1
2
3
4
5
6
7
8
9
10
11
12
// car.h
#ifndef CAR_H
#define CAR_H

class truck; // forward declare truck

struct car {
  int foo;
  car ( truck );  // We do not use members of truck here nor need to know its size.
  // It is enough to know that truck is a class
};
#endif 


1
2
3
4
5
6
7
// car.cpp
#include "car.h"
#include "truck.h"

car::car ( truck t ) // here we need the size of truck
 : foo( t.bar * 2 ) // here we refer members of truck
{}


@danicpp:
- Do not include the cpp into header.
- You still don't forward declare truck in car.h.
closed account (10X9216C)
1: If you include car alone anywhere without truck, you'll get a compiler error. So better to have the forward declaration.

2: What error, we can't help, or at least i'm not going to bother compiling your incomplete source. You still need to include the header files in the truck/car sources.
Last edited on
Why is it you can't follow instructions?

Several posts back I gave you the following instructions:

Change truck.h line 17 to pass by reference. Make sure car is declared forward.

Change car.h line 17 to pass truck by reference. Make sure truck is declared forward.


No one told you to include the .cpp files in the header. DO NOT DO THIS.

You must include both car.h and truck.h in both car.cpp and truck.cpp.



AbstractionAnon maybe you're looking my old code I changed the code and have uploaded in

my post: http://www.cplusplus.com/forum/general/139173/2/#msg737680
at: Jul 30, 2014 at 3:48am

and line 17 in truck.h in this new code is:
truck(car &);
and I believe in this line I'm passing car by reference using "&" operator

and line 17 in car.h in this new code is:
void print();
and in this line I haven't passed anything at all

I included .cpp in .h file because I thought how would compiler know where is the implementation code but again I guess by doing this I'm kicking the purpose of keeping declaration and implementation separate, BUT When I tried something like this

1. I excluded the #include car.cpp from car.h file and excluded #include truck.cpp from truck.h file

2. changed car.h now it starts like this


#ifndef car_h
#define car_h

class truck;

class car{

rest of the code is same (except I deleted #include car.cpp) as my new code post (only included a forward declaration of truck class)


3. changed car.cpp now it starts like this


#include "truck.h"
#include "car.h"

car::car(){

rest of the code is same (just included both truck.h and car.h)


4. changed truck.h now it start like this


#ifndef truck_h
#define truck_h

class car;

class truck{

rest of the code is same (except deleted #include truck.cpp)



#include "car.h"
#include "truck.h"

truck::truck(){

rest of the code is same


and this time compiler gives a weird error
just a few lines like this:

C:\Users\Dani\AppData\Local\Temp\cctVxOGS.o cartruck.cpp:(.text+0x52): undefined reference to `car::car()' {doesn't even mention a line no}

2nd last line is this:

c:\program files (x86)\dev-cpp\mingw64\x86_64-w64-mingw32\bin\ld.exe C:\Users\Dani\AppData\Local\Temp\cctVxOGS.o: bad reloc address 0x1 in section `.text$_ZN3carD1Ev[_ZN3carD1Ev]'

and last line is:

E:\Education\cpp\temp\cartruck\collect2.exe [Error] ld returned 1 exit status
AbstractionAnon maybe you're looking my old code I changed the code

Why would I look in your old thread for your current code? This is exactly why you should not open mutiple threads regarding the same problem.

and line 17 in car.h in this new code is: void print();

The line 17 I was referring to was your car constructor in the car.h in the second set of code on page 1 of this thread (2 posts above mine). Your car constructor needs to pass truck by reference.
 
    car (truck &);


C:\Users\Dani\AppData\Local\Temp\cctVxOGS.o cartruck.cpp:(.text+0x52): undefined reference to `car::car()' {doesn't even mention a line no}


It doesn't mention a line number because this is a linker error, not a compile error. The linker did not find your car constructor. Is car.cpp part of your project?



Last edited on
AbstractionAnon Please look at my new code [ same thread but on page two (I'm not using now the page 1 code) I'm using page 2 code of same thread and link to this new code is: http://www.cplusplus.com/forum/general/139173/2/#msg737680 ]

also please see I even tried changes in this new code too as you suggested like

I deleted includes of .cpp files in .h files also I forward declared in both car.h and truck.h files

I posted these changes in this new code in this post: http://www.cplusplus.com/forum/general/139173/2/#msg737926

Also maybe you've mentioned a new thing
Is car.cpp part of your project?

What do you mean by this.

I just launched the compiler and made new source files and put code in them and saved them like 1. cartruck.cpp 2. car.h 3. car.cpp 4 truck.h 5. truck.cpp and that's all, I didn't hit any project button or etc

BUT all of these files are in same folder

do I need to make some kind of project? can't these same files see each-other? They are in same folder
The following compiles and runs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//car.h
#ifndef car_h
#define car_h

class truck;		// forward added 

class car
{	
public:	
		std::string type;
		int year;
		
		car();	
		car(truck &);		
		void print();	
};
#endif  


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//truck.h
#ifndef truck_h
#define truck_h

class car;

class truck
{		
public:	
		std::string type;
		int year;
		
		truck();	
		truck(car &);		
		void print();	
};
#endif  


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//main.cpp
#include <iostream>
using namespace std;

#include "truck.h"
#include "car.h"

int main()
{	cout << "---------Program Initiated-----------" << endl << endl;
	
	car cobj;
	truck * ptrtruck = new truck(cobj);
	ptrtruck->print ();    // Added print

	truck tobj;
	car * ptrcar = new car(tobj);
	ptrcar->print ();    // Added print

	delete ptrtruck;
	delete ptrcar;
	
	system ("pause");
	return 0;	
}//---------main()    


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//car.cpp
#include <string>		//	Added include
#include <iostream>		//	Added include
#include "car.h"		//	Added include
#include "truck.h"		//	Added include 

car::car()
{	type = "Car";
	year = 2014;	
}//------constructor

car::car(truck & tobj)
{	type = "Car";
	year = tobj.year;
	
}//------------constructor(truck)

void car::print()
{	std::cout << "Type: " << type << "\tYear: " << year << std::endl;
}//---------print()  


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//truck.cpp
#include <string>		//	Added include
#include <iostream>		//	Added include
#include "car.h"		//	Added include
#include "truck.h"		//	Added include

truck::truck()
{	type = "Truck";
	year = 2012;	
}//------constructor()

truck::truck(car & cobj)
{	type = "Truck";
	year = cobj.year;		
}//--------constructor(car)

void truck::print()
{	std::cout << "Type: " << type << "\tYear: " << year << std::endl;	
}//----------print()    



---------Program Initiated-----------

Type: Truck Year: 2014
Type: Car Year: 2012
Press any key to continue . . .
Last edited on
all of these files are in same folder

do I need to make some kind of project?

The compilation of your code requires:
1. Compile object files from each of main.cpp, car.cpp, and truck.cpp
2. Link those three object files together (and add bits from libraries) in order to create the executable.

You are using some IDE, are you not?

IDE's have a concept of "project"; a list of cpp-files that have to be compiled to object files, libraries that are needed during linking, and other compiler options. When you have defined the project, the IDE's "Compile" button will do the necessary operations.

You don't have to have all files of a project in same directory. You can have files for several projects in same directory and you can use same file in multiple projects. You would not have these options without "projects".

How you modify project configuration in your IDE -- Frankly, my dear, I don't ...
Last edited on
I knew it the code was just alright in my last attempt but there is either something xyz(can't use vulgar language here) wrong with my orwell/dev compiler or with my knowledge (maybe I'm missing something about project thing)

AbstractionAnon you know what I did now, I just deleted all my code and copied this code which you said works fine and saved and compiled and you see what happened???


C:\Users\Dani\AppData\Local\Temp\cckroGne.o cartruck.cpp:(.text+0x52): undefined reference to `car::car()'
C:\Users\Dani\AppData\Local\Temp\cckroGne.o cartruck.cpp:(.text+0x6e): undefined reference to `truck::truck(car&)'
C:\Users\Dani\AppData\Local\Temp\cckroGne.o cartruck.cpp:(.text+0x7e): undefined reference to `truck::print()'
C:\Users\Dani\AppData\Local\Temp\cckroGne.o cartruck.cpp:(.text+0x8a): undefined reference to `truck::truck()'
C:\Users\Dani\AppData\Local\Temp\cckroGne.o cartruck.cpp:(.text+0xa6): undefined reference to `car::car(truck&)'
C:\Users\Dani\AppData\Local\Temp\cckroGne.o cartruck.cpp:(.text+0xb6): undefined reference to `car::print()'
c:\program files (x86)\dev-cpp\mingw64\x86_64-w64-mingw32\bin\ld.exe C:\Users\Dani\AppData\Local\Temp\cckroGne.o: bad reloc address 0x1 in section `.text$_ZN3carD1Ev[_ZN3carD1Ev]'
E:\Education\cpp\temp\cartruck\collect2.exe [Error] ld returned 1 exit status
Your linker errors show that car.o (the object of car.cpp) and truck.o (the object of truck.cpp) did not get linked into your executable.
Guys YESSSSS!!!!!!!!!!!!!!!! I did it, I did it guys thanks to every one of you, I just love you all, Thankyou very much

What I did is:

1. closed all the files
2. started new project with a main file
3. copied the code for main file in main
4. created new classes inside the project named car and truck and IDE made by itself for me two files for each of them one .h and the other .cpp and amazingly .h file already had include guards lolx...
5. copied respective code
6. saved all the files and compiled

7. pressed F10 and pufffffffffff............. THE magic just happened it worked fine

I'm so happy, now can can start my work on converting objects between different classes

Thanks to AbstractionAnon

Thanks to keskiverto

Thanks to ne555

Thanks to myesolar

Learning summary:

1. better and sometimes necessary way to pass an object of another user-defined class is to pass by reference or pointer rather than passing by value

2. if passing reference/pointer of an other class then forward declare (that other class)

3. don't just fire up the compiler and keep on making files------- if files need to work together then make a project

The guerrilla fighter of the problem was: Project (I should have spotted him earlier)
Topic archived. No new replies allowed.
Pages: 12