Friend function: Remove this 1 error.

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
#include <iostream>
using namespace std;

class a{
	friend int operator+(int callingInt, a& obj);
	
	int x; //private
public:
	int getX(){return x;}
	void setX(int a){
		x = a;
	}
};

int operator+(int callingInt, a& obj){
	return (callingInt + obj.x);
}

int main(){
	a obj1, obj2;
	obj1.setX(5);//Functions are called over objects.
	cout << "x = " << obj1.getX() << endl;
	/*obj2.setX(10);
	cout << "x = " << obj2.getX() << endl;*/

	cout << "10 + obj1 = " << 10 + obj1 << endl;
	return 0;
}
Where and what this error is?
Is this what you were trying to do:

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
#include <iostream>
using namespace std;

class a
{
public:
	a() : x(0)
	{
	}

	a (const int val) : x(val)
	{
	}

	a& operator+(const a& obj)
	{
		x += obj.x;
		return *this;
	}

	friend ostream& operator << (ostream& os, a& obj)
	{
		os << obj.x;
		return os;
	}
	
	int getX()
	{
		return x;
	}

	void setX(int a)
	{
		x = a;
	}

private:
	int x;
};

int main()
{
	a obj1(5), obj2;
	cout << "x = " << obj1.getX() << endl;
	/*obj2.setX(10);
	cout << "x = " << obj2.getX() << endl;*/

	cout << "obj1 + 10 = " << obj1 +10 << endl;
	return 0;
}
I want to give prototype in the class and definition of friend function out of class. Will someone please help me?
Your code working exactly as you want.
Thanks, but why this code isn't running on Microsoft Visual C++ 6.0?
post errors here

Edit:
http://bytes.com/topic/c/answers/130616-c-friend-function

Anytime the words "friend function", "strange error" and "MSVC 6" are
uttered together, there's a good chance you're dealing with the infernal
"friend" bug in VC6. If you haven't yet, go download the latest service
pack (it is up to Service Pack 6 now) at:

http://msdn.microsoft.com/en-US/vstudio/downloads/updates/sp/vs6/sp6/default.aspx

What compiler is it, and what version? I gather that with Visual C++,
anything before 7.1 was pretty crappy.


http://groups.google.com/forum/?fromgroups=#!topic/comp.lang.c++/bKIbGnoUBfY
This is an error acknowledged by Microsoft. The latest issue of the C++
Report contains a Microsoft support article number where you can find
a fix. However, there is a workaround: Skip saying using namespace std
and instead say using std::string, using std::vector etc.,
in brief
list explicitly all the std facilities you are about to use in your program.
Last edited on
Error which I see:

C:\Program Files (x86)\Microsoft Visual Studio\MyProjects\nnnnnnnnn\nnn.cpp(5) : fatal error C1001: INTERNAL COMPILER ERROR
(compiler file 'msc1.cpp', line 1786)
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information
Error executing cl.exe.

nnn.obj - 1 error(s), 0 warning(s)
See my previous message: problem with compiler.
You can try the workaround in last quote, find service pack on Microsoft website (link posted in quote isn't working), upgrade it to newer version, or try alternative compilers.
You should consider using software from the last few years, such as VC++2008. I know, 2008 is a huge step forward, and you're probably not comfortable to make the enormous leap, but trust me: you should, it will be worth it.
Thanks to LB, and special thanks to MiiNiPaa.
Last edited on
Haha, glad my satire and hyperbole can be helpful.
Topic archived. No new replies allowed.