c++ friend operator and Classes

I need help with a few things... If someone can PM me, I would sincerely appreciate it.

TY
That's not how forums work.
Post the problem here and people will give an answer.
Trying to declare stand-alone friend function, and everything is complaining. Public, private, outside of scope and right below the Class declaration. Still have compiler complaints.
Last edited on
Put the whole code
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream>

using namespace std;

class Number
{	
	friend void Reset(Number &t);
private:
	int num;
	int twiceNum;
	
public:
	Number(int n = 0);
	Number(const Number & n);
	void setNum(int n);
	void setTwiceNum(int tn);
	int getNum() const;
	int getTwiceNum() const;
	Number operator+ (const Number & r);
	Number operator++ ();
	Number operator++ (int);
};
Number::Number(int n = 0)
{
	setNum(n);
}
Number::Number(const Number & n)
{
	setNum(num = 0 & twiceNum);
}
void Number::setNum(int n)
{
	n = num;
	twiceNum = 2 * num;
}
void Number::setTwiceNum(int tn)
{
	if (tn % 2 == 0)
	{
		tn = twiceNum;
	}
	else
	{
		twiceNum = 0;
	}
	num = twiceNum / 2;
}
int Number::getNum() const
{
	return num;
}
int Number::getTwiceNum() const
{
	return twiceNum;
}
Number Number::operator+ (const Number & r)
{
	/*r;
	setNum;
	return 0;*/
}
Number Number::operator++ ()
{
	/*num = num += 1;
	setNum;*/
}
Number Number::operator++ (int)
{
	/*num = num + 1;
	setNum;*/
}
friend void Number::Reset(Number &t)   //friend
{
	Number::num = 0;
	Number::twiceNum = 0;
}
int main()
{
	Number a, b(5), c(10);
	Number d=c;
	Number e;
	cout<<"a: " <<a.getNum() << ", 2 X a: "<<a.getTwiceNum()<<endl;
	cout<<"b: " <<b.getNum() << ", 2 X b: "<<b.getTwiceNum()<<endl;
	cout<<"c: " <<c.getNum() << ", 2 X c: "<<c.getTwiceNum()<<endl;
	cout<<"d: " <<d.getNum() << ", 2 X d: "<<d.getTwiceNum()<<endl;
	cout <<endl;
	Reset(b);
	cout <<"After reset of b"<<endl;
	cout<<"b: " <<b.getNum() << ", 2 X b: "<<b.getTwiceNum()<<endl;
	cout <<endl;
	b.setNum(7);
	cout <<"After b.setNum(7)"<<endl;
	cout<<"b: " <<b.getNum() << ", 2 X b: "<<b.getTwiceNum()<<endl;
	cout <<endl;
	b.setTwiceNum(21);
	cout <<"After b.setTwiceNum(21)"<<endl;
	cout<<"b: " <<b.getNum() << ", 2 X b: "<<b.getTwiceNum()<<endl;
	cout <<endl;
	b.setTwiceNum(30);
	cout <<"After b.setTwiceNum(30)"<<endl;
	cout<<"b: " <<b.getNum() << ", 2 X b: "<<b.getTwiceNum()<<endl;
	cout <<endl;
	a= b + c;
	cout <<"After a= b+c"<<endl;
	cout<<"a: " <<a.getNum() << ", 2 X a: "<<a.getTwiceNum()<<endl;
	cout<<"b: " <<b.getNum() << ", 2 X b: "<<b.getTwiceNum()<<endl;
	cout<<"c: " <<c.getNum() << ", 2 X c: "<<c.getTwiceNum()<<endl;
	cout <<endl;
	e= ++a;
	cout <<"After e= ++a"<<endl;
	cout<<"a: " <<a.getNum() << ", 2 X a: "<<a.getTwiceNum()<<endl;
	cout<<"e: " <<e.getNum() << ", 2 X e: "<<e.getTwiceNum()<<endl;
	cout <<endl;
	e= a++;
	cout <<"After e= a++"<<endl;
	cout<<"a: " <<a.getNum() << ", 2 X a: "<<a.getTwiceNum()<<endl;
	cout<<"e: " <<e.getNum() << ", 2 X e: "<<e.getTwiceNum()<<endl;
	cout <<endl;
	return 0;
}


friend functions are not member functions (if they were, they wouldn't need to be friends with the class in the first place), so:
void Reset(Number &t)

This is not going to work either, because num and twiceNum aren't static members:
1
2
Number::num = 0;
Number::twiceNum = 0;


Why are you trying to make it a friend function anyway? Either make it a member function or use the public interface (setNum...) to reset the values - there is no need for the friend keyword here.

Also, you can't specify a default value both in the declaration and definition of a function:
1
2
Number(int n = 0);
Number::Number(int n = 0) //wrong 
Last edited on
When I try and compile, these are the errors I get:
1>Snippets.cpp(24): error C2572: 'Number::Number' : redefinition of default parameter : parameter 1
1>          Snippets.cpp(13) : see declaration of 'Number::Number'
1>Snippets.cpp(72): error C2255: 'friend' : not allowed outside of a class definition
1>Snippets.cpp(72): error C2039: 'Reset' : is not a member of 'Number'
1>          Snippets.cpp(6) : see declaration of 'Number'
1>Snippets.cpp(74): error C2597: illegal reference to non-static member 'Number::num'
1>Snippets.cpp(75): error C2597: illegal reference to non-static member 'Number::twiceNum'


1) you can only define a default value for a function in the declaration.
change: Number::Number(int n = 0)
to: Number::Number(int n)

2) friend only needs to be used inside of a class. Outside it doesn't matter.
change: friend void Number::Reset(Number &t)
to: void Number::Reset(Number &t)

3) Now we come accross another problem. Reset is not a member of Number. If it was, then we wouldn't need the friend function. Because it isn't a member, we should do this:
void Reset(Number &t)

4&5) Since Reset is declared as a friend in the Number class, it can access the private members of t as if they were public. That means that we don't need those funny Number:: scopes which is causing 2 more errors. Change the whole function to:
1
2
3
4
5
void Reset(Number &t)   //friend
{
	t.num = 0;
	t.twiceNum = 0;
}

Last edited on
You should read about friend functions because you have wrong ideas about it
Friend functions ARE NOT MEMBER FUNCTIONS so you cannot define it by specifing
classname::
before it.
So you should define it in such way:

1
2
3
4
 void Reset(Number &t)   //friend
{
	//Function body
}
But there are a lot of other mistakes in your code.
Read it carefully again.

1.In friend function body you have written



1
2
Number::num = 0;
Number::twiceNum = 0;


but num and twicenum are not static members.
2.operator +, operator ++ functions do not return values.
3.You've redefined default argument in constructor
Last edited on
To help you out with other errors that will appear...

Once I made the corrections above, I tried to compile and found that you haven't finished your operator++ functions. If you don't want help, then stop reading.

Otherwise:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Number Number::operator+ (const Number & r)
{
	Number plus;
	plus.num = num + r.num;
	plus.twiceNum = twiceNum + r.twiceNum;
	return plus;
}
Number Number::operator++ ()
{
	num++;
	return *this;
}
Number Number::operator++ (int)
{
	Number old(*this);
	num++;
	return old;
}

Last edited on
Topic archived. No new replies allowed.