Call by reference

I am having a code from three pieces
Header and two cpp classes.
I want to know is my structure is correct and why is not working. I got no error but it's just break.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
User.cpp
#include <iostream>
#include "User.h"
using namespace std;
void Create(& user)
{
	cout<<"Enter your Name"<<endl;
}
main.cpp
#include <iostream>
#include "User.h"
using namespace std;

int main()
{
	User user;
user.Create(user);  //// passing object to the function
}
Header.h
class User
{
public:
    void Create(User&);


Thanks in advance
Last edited on
> I got no error but it's just break.
¿what do you mean with break?
your program will print one line to the screen and end.


> user.Create(user);
¿don't you find that ugly and error prone?
Hi

It's not printing anything.
I think create function cannot see the function implementation.
Last edited on
That's not exactly how it works, you have to explicitly make the function definition part of the class.

1
2
3
4
5
6
class User
{
public:
    void Create(User&);

};


1
2
3
4
void User::Create(User& user) // notice User:: and the parameter change, you need to give it a type...
{
	cout<<"Enter your Name"<<endl;
}


http://www.cplusplus.com/doc/tutorial/classes/
Last edited on
This is the full code:

main.cpp
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
#include <iostream>
#include "User.h"
using namespace std;
int main()
{
	User user;
	while(true)
	{
	string option;
	cout<<"Please Enter a command ( Create , Update , View , Favoriate, Quit "<<endl;
	cin>> option;
			if(option == "Create")
			{
			    user.Create(user);  //// passing object to the funcion
			}

			if(option == "Update")
			{
			    user.Update(user);
			}

			if(option == "View")
			{
				user.View(user);   //// passing object to the funcion
			   // setUserName(User &user, string f, string l);
			}
			if(option == "Favoriate")
			{
			 	user.Favoriate(user);   //// passing object to the funcion
			}

			if(option == "Quit")
			{
				cout<<"You Exit from the Program "<<endl;
				break;			    
			}
	}
	return 0;
}
User.cpp

#include <iostream>
#include "User.h"
using namespace std;
void User::Create(User user)
{
//	int age;
//	string num[5];

//	string fname;
//	string lname;
	//int name,age;char num[5];
	cout<<"Enter first Name"<<endl;
	cin>>fname;
	cout<<"Enter first Name"<<endl;
	cin>>lname;
	cout<<"Your Age"<<endl;
	cin>> age;
}

void User::Update(User user)
{
	string num[5];

	for(int i = 0 ;i< 5 ; i++)
	{

		cout<<num[i]<<endl;
	}

}

void User::View(User user)
{
	string num[5];

	for(int i = 0 ;i< 5 ; i++)
	{

		cout<<num[i]<<endl;
	}
}

void User::Favoriate(User user)
{
	string num[5];

	for(int i = 0; i< 5 ; i++)
	{
		cout<<"What is your favoriate Movies"<<endl;
		cin>>num[i];
	}
}

User.h
#ifndef USER_H
#define USER_H



	std::string num[5];
	std::string fname;
	std::string lname;
	int age;

class User
{
	public:
		//int age;
		//std::string num[5];
		//std::string fname;
		//std::string lname;
		void Update(User user);
		void View(User user);
		void Create(User user);
		void Favoriate(User user);
		std::string setname();


};
#endif 


When I put the create and passed the number then choose View, I see nothing. It's not saving the number
Last edited on
I think you're having some trouble understanding how classes work. ALthough there is only one User in your main function, conceptually, each User has an age, a first & last name, and a list of favorite movies. To express this, you declare those members inside the class User declaration:
1
2
3
4
5
6
7
8
class User
{
  public:
    int age;
    std::string num[5];
    std::string fname;
    std::string lname;
...


The next point is the way that methods work. When you call a method of a class, you don't have to pass the class instance that you want to act upon. In other words, you don't have to say user.Create(user). You can just say user.Create(). Then the create method can just be:
1
2
3
4
5
6
7
8
9
10
void
User::Create()
{
    cout << "Enter first Name" << endl;
    cin >> fname;
    cout << "Enter first Name" << endl;
    cin >> lname;
    cout << "Your Age" << endl;
    cin >> age;
}

Here fname, lname and age refer to the member variables of class User. So if you call user1.Create(), then inside the Create() method, fname refers to user1.fname, lname refers to user1.lname and age refers to user1.age. If you call user2.Create() then inside Create(), the names refer to user2.lname, user2.fname and user2.age.
So you can declare the Create method liket this:
1
2
3
4
5
6
class User
{
...
    void Create();
...
};

And you define it like this:
1
2
3
4
5
6
7
8
9
void User::Create()
{
    cout << "Enter first Name" << endl;
    cin >> fname;
    cout << "Enter first Name" << endl;
    cin >> lname;
    cout << "Your Age" << endl;
    cin >> age;
}


Make sense?

So you want to declare class User like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
class User
{
  public:
    int age;
    std::string num[5];
    std::string fname;
    std::string lname;
    void Update();
    void View();
    void Create();
    void Favoriate();;
    std::string setname();
};

and get rid of the global variables define at lines 101-104 of your last post.

See if you can define the methods using what you know about how Create() works. You will also have to change the way you call them in main().
Hi dhaydan ,

I want to call function not method.
1
2
3
4
5
6
 I want the function implementation to be look like this
  void Create(&user)
{ 
  ////////
 
}


so the function is defined outside the User class and called by passing an object to the function &objectName.

Here is the last version of my 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
124
125
126
127
128
129
130
131
132
133
134
135
#include <iostream>
#include "User.h"
using namespace std;




int main()
{
	User user;
	while(true)
	{

	string option;
	cout<<"Please Enter a command ( Create , Update , View , Favoriate, Quit "<<endl;
	cin>> option;


			if(option == "Create")
			{
			    Create(user);  //// passing object to the funcion
			}

			if(option == "Update")
			{
			    Update();
			}

			if(option == "View")
			{
				View();   //// passing object to the funcion
			   // setUserName(User &user, string f, string l);
			}
			if(option == "Favoriate")
			{
			 	Favoriate();   //// passing object to the funcion
			}

			if(option == "Quit")
			{
				cout<<"You Exit from the Program "<<endl;
				break;			    
			}


	}

	return 0;
}

User.cpp
#include <iostream>
#include "User.h"
using namespace std;


void Create(User& user)
{
//	int age;
//	string num[5];

//	string fname;
//	string lname;
	//int name,age;char num[5];
    //user = fname;
	cout<<"Enter first Name"<<endl;
	cin>>fname;
	cout<<"Enter first Name"<<endl;
	cin>>lname;
	cout<<"Your Age"<<endl;
	cin>> age;
	cout<<"Enter your movies"<<endl;
	for(int i = 0 ;i< 5 ; i++)
	{

		cin>>num[i];
		cout<<endl;
	}

}

void Update(User* user)
{
	string num[5];

	for(int i = 0 ;i< 5 ; i++)
	{

		cout<<num[i]<<endl;
	}

}

void View(User* user)
{
	string num[5];

	for(int i = 0 ;i< 5 ; i++)
	{

		cout<<num[i]<<endl;
	}
}

void Favoriate(User* user)
{
	string num[5];

	for(int i = 0; i< 5 ; i++)
	{
		cout<<"What is your favoriate Movies"<<endl;
		cin>>num[i];
	}
}

User.h
#ifndef USER_H
#define USER_H
using namespace std;

string num[5];
string fname;
string lname;
int age;
void Update();
void View();
void Create();
void Favoriate();

class User
{
	public:
		std::string setname();
};
#endif 
Last edited on
If Create() is outside of the User class and num and fname and lname and age are all outside the User class, then why do you have a User class?
I am not sure why but the whole point is that, I want to call the function create() from outside the class User and that's why i am creating the class.

Like this
1
2
3
4
5
6
7
8
9
10
void Create(&user)
{
 //// 
}

and main.cpp
{
  User user 
  user.Create(user);
}
If you want Create() to be outside the user class, then you'd call it like this:
1
2
3
4
5
6
7
8
9
10
void Create(&user)
{
 //// 
}

and main.cpp
{
  User user 
  Create(user);
}

and the code would look like this:
1
2
3
4
5
6
7
8
9
10
void
Create(User &user)
{
    cout << "Enter first Name" << endl;
    cin >> user.fname;
    cout << "Enter first Name" << endl;
    cin >> user.lname;
    cout << "Your Age" << endl;
    cin >> user.age;
}

But it makes no sense to do it that way. Cases like this are precisely WHY we have methods for classes.
Hi,

I made the change and i got this errors

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

lab2.cpp:21:13: error: no member named 'Create' in 'User'
                            user.Create(user);  //// passing object to t...
                            ~~~~ ^
lab2.cpp:26:13: error: no member named 'Update' in 'User'
                            user.Update(user);
                            ~~~~ ^
lab2.cpp:31:10: error: no member named 'View' in 'User'
                                user.View(user);   //// passing object t...
                                ~~~~ ^
lab2.cpp:36:11: error: no member named 'Favoriate' in 'User'
                                user.Favoriate(user);   //// passing obj...
                                ~~~~ ^




This si the 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159

main.cpp

#include <iostream>
#include "User.h"
using namespace std;




int main()
{
	User user;
	while(true)
	{

	string option;
	cout<<"Please Enter a command ( Create , Update , View , Favoriate, Quit "<<endl;
	cin>> option;


			if(option == "Create")
			{
			    user.Create(user);  //// passing object to the funcion
			}

			if(option == "Update")
			{
			    user.Update(user);
			}

			if(option == "View")
			{
				user.View(user);   //// passing object to the funcion
			   // setUserName(User &user, string f, string l);
			}
			if(option == "Favoriate")
			{
			 	user.Favoriate(user);   //// passing object to the funcion
			}

			if(option == "Quit")
			{
				cout<<"You Exit from the Program "<<endl;
				break;			    
			}


	}

	return 0;
}

User.cpp

#include <iostream>
#include "User.h"
using namespace std;


void 
Create(User &user)
{
//	int age;
//	string num[5];

//	string fname;
//	string lname;
	//int name,age;char num[5];
    //user = fname;
	cout<<"Enter first Name"<<endl;
	cin>>user.fname;
	cout<<"Enter first Name"<<endl;
	cin>>user.lname;
	cout<<"Your Age"<<endl;
	cin>> user.age;
	cout<<"Enter your movies"<<endl;
	for(int i = 0 ;i< 5 ; i++)
	{

		cin>>user.num[i];
		cout<<endl;
	}

}

void 
Update(User &user)
{
	string num[5];

	for(int i = 0 ;i< 5 ; i++)
	{

		cout<<num[i]<<endl;
	}

}

void 
View(User &user)
{
	string num[5];

	for(int i = 0 ;i< 5 ; i++)
	{

		cout<<num[i]<<endl;
	}
}

void Favoriate(User &user)
{
	string num[5];

	for(int i = 0; i< 5 ; i++)
	{
		cout<<"What is your favoriate Movies"<<endl;
		cin>>num[i];
	}
}


User.h

#ifndef USER_H
#define USER_H
using namespace std;

string num[5];
string fname;
string lname;
int age;
void Update();
void View();
void Create();
void Favoriate();

class User
{
	
	//std::string num[5];
	//std::string fname;
	//std::string lname;
	//int age;
	public:
		//int age;
		//std::string num[5];
		//std::string fname;
		//std::string lname;
		//void Update(User user);
		//void View(User user);
		//void Create(User user);
		//void Favoriate(User user);
		std::string setname();


};
#endif 

lab2.cpp:21:13: error: no member named 'Create' in 'User'
user.Create(user); //// passing object to t...

Well you said you wanted to call Create using Create(user). So you need to call it that way.

lab2.cpp:26:13: error: no member named 'Update' in 'User'
user.Update(user);
~~~~ ^
lab2.cpp:31:10: error: no member named 'View' in 'User'
user.View(user); //// passing object t...
~~~~ ^
lab2.cpp:36:11: error: no member named 'Favoriate' in 'User'
user.Favoriate(user); //// passing obj...

And presumably you want to call Update, View and Favoriate() that way too.

Also, in User.h, you still have num, fname, lname, and age as global variables rather than members of class User. Shouldn't they be members??
Topic archived. No new replies allowed.