trouble with class constructor

hi. I have a trouble with my class constructor.

my code:
1
2
3
4
5
6
7
8
class User{
  private :
	std::string displayname;
	std::string username;
	std::string password;
  public :
    User(std::string username, std::string displayname, std::string password)
};


and somewhere in main I try to construct an object of user like this:

User user(username, displayName, password);
(username , displayname and password are strings that I've checked them and they are ok.)

when I try to print this user's information there is nothing to be printed. It's somehow like the constructor doesn't store any of this strings. Could u plz tell me how to solve this? :(
I'd appreciate your helps on it. thanks :)
Last edited on
Well just to make sure (since I'm not sure if you just didn't post the full constructor), in the constructor's body there needs the actual parameters need to be assigned to the private members.
You didn't actually show what's inside your constructor, User user(username, displayName, password); merely tells your program what the arguments for your constructor are. You need to define your function outside the class, like so for instance:
1
2
3
4
5
6
User::User(std::string name, std::string display, std::string pw)
{
     displayname = display;
     userName = name;
     password = pw;
}


1
2
3
4
5
6
int main()
{
     User user("Bob", "Mr Bob", "mypassword");

     return(0);
}


I hope this helps!

Hugo.
thank u all for your help.
I just forgot to write the body of my constructor. :)))
now it seems to be fine.
thanks again.
EDIT: Here's a little driver program I made quickly to show you what I mean
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
#include <iostream>

void pause();


class User
{
  private :
	std::string displayName;
	std::string userName;
	std::string password;
  public :
    User(std::string, std::string, std::string); //constructor forward declaration
    ~User();
    
    void displayInfo();
};

int main()
{

	User user("Bob", "Mr Bob", "mypassword123");
	
	user.displayInfo();
	
	pause();
	
	
	return(0);
}


void pause()
{
	std::cout << "\n\n\n Press enter to continue...";
	std::cin.sync();
	std::cin.get();
	std::cout << "\n\n";
	
	return;
}

User::User(std::string name, std::string display, std::string pw) //constructor definition
{
	displayName = display;
	userName = name;
	password = pw;
}

User::~User()
{}

void User::displayInfo()
{
	std::cout << "\n\n User name: " << userName
			  << "\n Display name: " << displayName
			  << "\n Password: " << password;
			  
	return;
}
thank u so much this really helped! :)
My Pleasure :)
Topic archived. No new replies allowed.