i am getting the error here.

this is a visual studio console application.
there is two error.
1. expected type specifier.
2. int passbyref(int&)cannot convert argument 1 from int* to int &.

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
  // pointer lab.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
int passbyvalue(int z);
int passbyref(int &r);
class person {
public:
	int age, weight, height;
	string name;
};
person modifyperson(person p);


int main()
{
	int num1,pnum;
	double x;
	//here z is pointer
	 int *z = &pnum;
	double *c = new  &x;
	*c = 8.02;
	num1 = 3;
	*z = 5;
	passbyvalue(num1);
	cout << "The value of num 1" << num1 << endl;
	passbyref(pnum);
	cout << "The value of the double pointer is " << *c << endl;
	delete c;
	cout << "Again trying to get the value of the double pointer it is " << *c << endl;
	person p1;
	p1.age = 21;
	p1.name = "Kumar Aditya";
	p1.weight = 65;
	p1.height = 6;
	cout << "The all details sotred for the person p1 is " << endl;
	cout << "Age " << p1.age << endl;
	cout << "Name " << p1.name << endl;
	cout << "weight " << p1.weight << endl;
	cout << "height " << p1.height << endl;
	modifyperson(p1);
	cout << "After the modifyperson function the value inside the object are " << endl;
	cout << "Age " << p1.age << endl;
	cout << "Name " << p1.name << endl;
	cout << "Weight " << p1.weight << endl;
	cout << "Height " << p1.height << endl;
 return 0;
}
int passbyvalue(int z)
{
	cout << "We are in pass by value function" << endl;
	z++;
	cout << "the new value of z is " << endl;
	return z;

}
int passbyref(int &a)
{
	cout << "We are in pass by ref function " << endl;
	a = 50;
	cout << "the value of pnum is " << endl;
	return a;
}
person modifyperson(person p)
{
	cout << "Inside the modifyperson function";
	p.name = "aditya";
	p.age = 14;
	p.weight = 70;
	p.height = 5;


}
The second cited error doesn't correspond to the code that you have posted. Please test your code in c++ shell (where you will need to comment out the #include "stdafx.h" line).


Line 24:
double *c = new &x;
should be
double *c = new double;
That will remove the first error.



Your usage of modifyperson() in main() suggests that this should be a void function; also, you have no return statement within this function anyway. Since you do presumably want to modify a person the function argument needs to be passed by reference rather than value. I suggest that you change the function declaration to
void modifyperson(person &p)
Note:
- the "void";
- the &
- the need to change it both in the declaration at the top of your code and the function definition.
Last edited on
Topic archived. No new replies allowed.