friend functions

below is a code where a function b() is friend of classes sample1 and sample2.
another function c() is a member of class sample1 but a friend function of class sample2

the code is not running ..what is the mistake?

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<iostream.h>
#include<conio.h>

class sample2;
class sample1
{
      int i1,j1;              
      
      public:
             void a1()
             {
              i1=1;j1=2;
             }   
             friend void b(sample1 obj1,sample2 obj2);
             void c(sample1 obj1,sample2 obj2)
             {
                  printf("%d %d %d %d ",obj1.i1,obj1.j1,obj2.i2,obj2.j2);
             }
};
class sample2
{
      int i2,j2;
      
      public:
             void a2()
             {
                  i2=3;j2=4;
             }
             friend void b(sample1 obj1,sample2 obj2);
             friend void sample1::c(sample1 obj1,sample2 obj2);
};

void b(sample1 obj1,sample2 obj2)
{
     printf("%d %d %d %d",obj1.i1,obj1.j1,obj2.i2,obj2.j2);
}


int main()
{
    sample1 obj1,obj3;
    sample2 obj2;
    obj1.a1();
    obj2.a2();
    b(obj1,obj2);
    obj3.c(obj1,obj2);           
    getch();
     
}


errors are:

16. 'obj2' has incomplete type
4. forward declaration of `struct sample2'
17 invalid use of undefined type `struct sample2'
Last edited on
This post explains it well: http://stackoverflow.com/questions/553682/when-to-use-forward-declaration

You would have to declare your classes in a header file and define them in a separate file in order to do what you want.
You are defining function c

1
2
3
4
             void c(sample1 obj1,sample2 obj2)
             {
                  printf("%d %d %d %d ",obj1.i1,obj1.j1,obj2.i2,obj2.j2);
             }



and trying to access variables obj2.j2 and obj2.j2 of class sample2 but the compiler knows nothing about members of sample2 except that sample2 is a class.

class sample2;

You must define this function after the definition of sample2.
You can't metion a type unless you've declared it. The compiler needs the definition so it can check that you're using it correctly.

However, you can forward declare types if you only use pointers or references to them because the compiler knows how large a pointer is. You still need the full definition before it can be used. That's why sample1::c cannot be defined in the class body.

In practice, you very rarely need the actual type. Often you (should) use a reference or pointer to a type.

For example, your friend function really could (and maybe should) be declared as:
 
void b(const sample1 &obj1, const sample2 &obj2);

Let's revisit what you've done using forward declarations and indirection where possible.
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>

class sample1; // not strictly necessary
class sample2; // used in sample1

class sample1
{
friend void b(const sample1 &obj1, const sample2 &obj2); // use const reference

	int i1;
	int j1;
      
public:
	void a1()
	{
		i1 = 1;
		j1 = 2;
	}

	void c(const sample1 &obj1, const sample2 &obj2); // use const reference
};

class sample2
{
friend void b(const sample1 &obj1, const sample2 &obj2); // use const reference
friend void sample1::c(const sample1 &obj1, const sample2 &obj2); // use const reference

	int i2;
	int j2;

public:
	void a2()
	{
		i2 = 3;
		j2 = 4;
	}
};

// can't use sample2 until it's been declared, so this can't be inline
void sample1::c(const sample1 &obj1, const sample2 &obj2)
{
	std::cout
		<< obj1.i1 << ' ' << obj1.j1 << ' '
		<< obj2.i2 << ' ' << obj2.j2 << std::endl;
}

void b(const sample1 &obj1, const sample2 &obj2)
{
	std::cout
		<< obj1.i1 << ' ' << obj1.j1 << ' '
		<< obj2.i2 << ' ' << obj2.j2 << std::endl;
}

int main()
{
	sample1 obj1, obj3;
	sample2 obj2;
	obj1.a1();
	obj2.a2();

	b(obj1, obj2);
	obj3.c(obj1, obj2);

	return 0;
}
Last edited on
tried everything..but not able to get perfect code..could any1 plz edit the code so that it runs perfectly!
What's wrong with what I posted?

I noticed you have an old compiler. If your compile fails on
 
#include <iostream> 
Use
 
#include <iostream.h> 
and remove all instances of the string
 
std::
Last edited on
actually u edited ur post after i posted my rply...that led to misunderstanding

well.. nw the code is running...thnx so much..u rock!!!..by the way what is the need to use const keyword everywhere??..without it also..code is running..
Last edited on
@kbw..u seem to be quite experienced in world of programming..(5018 posts..ooh)..

well..i need a suggestion..i just know c/c++ and i need to learn java and game programming as soon as possible( coz i wanna do internship..ie teach students in school the same ...which is starting from OCT 1)..do u know hw much time this java and game programming will take and hw difficult it is as compared to c/c++..??

thank u
actually u edited ur post after i posted my rply...that led to misunderstanding
I edited the text, not the code.

by the way what is the need to use const keyword
In C there is only one way to pass parameters to a function, pass by value.
http://en.wikipedia.org/wiki/Pass_by_value#Call_by_value

In C, you implement pass by reference by passing the address of the variable yourself and dereferencing it in the function yourself. You can do this in C++ too.

C++ introduced pass by reference, which allows does the same as passing the address of the variable without you having to deal with all the clunky syntax. Plus, you know that a reference is always bound to a variable, whereas a pointer may or may not be bound to something.

Passing by const reference is better than passing by value because you don't incur the cost of copying the object to the temporary copy of the object to the stack. And you also get a read-only thing, so you can't modify it. So it's the preferred way of passing read-only objects to functions.

do u know hw much time this java and game programming will take and hw difficult it is as compared to c/c++..??
C is a simple language with very few rules, supports the procedural paradigm and pushes most of the functionality into the standard library.

C++ is an incredibly complicated language. It supports the procedural and object oriented paradigm. And it comes with a really cool library, the standard template library.

Java is a much simpler language than C++. It only supports the object oriented paradigm, but has a more sophisticated object model than C++ which support more run-time discovery and aspect oriented programming. There are many more de-facto standard libraries and frameworks for Java.

If you don't know about pass by value ... you probably really don't know that much about C++. It's much easier to become an expert at Java. Much of its use is more self-evident.
Topic archived. No new replies allowed.