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 &.
// pointer lab.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<string>
usingnamespace 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 = newdouble;
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.