Help with using a type alias with a custom class. Please.

I think I answered my own question during the time it took me to write this out, but I want to make sure I'm right. I left the subject the same even though I could of changed it to something more specific seeing that I figured it out. The important stuff is at the end code the code here. I commented the code.

I was getting an error related to invalid arguments. I had a hunch that is had something to do with my class constructor. Seeing what I did below you should pick up what the mistake was.

My explaination is that:
1
2
3
4
5
        //This is creating a vector, of type User_Data_Class, named User.
        vector <User_Data_Class> User;

        //This is creating a User_Data_Class class object named New_Entry with arguments n, p, and a.
        User_Data_Class New_Entry {n, p, a}; 


So I was creating a vector object, of type User_Data_Class, named New_Entry, and I was trying to send arguments that object had no clue about.

Is this right?

Thanks.


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
//Classes:
class User_Data_Class{
	private:
		string Name;
		string Pass;
		string Access;

	public:
		User_Data_Class();
		User_Data_Class(string n, string p, string a) : Name(n), Pass(p), Access(a){};

		string Get_Name(){return Name;}
		string Get_Pass(){return Pass;}
		string Get_Access(){return Access;}

};





//Type Aliases:
using User_Data = vector <User_Data_Class>;






//Function Prototypes:
void Provide_User_Access(User_Data&);
void New_User_Entry(string, string, string, User_Data&);






int main(){
	User_Data User;
	//vector <User_Data> User;

	//Assemble the user vector for user verification.
	Provide_User_Access(User);

}






void Provide_User_Access(User_Data &User){

	//Users to be added.
	New_User_Entry("c", "c123", "creator", User);

}





//I'm getting an invalid arugments error with this:

void New_User_Entry(string n, string p, string a, User_Data &User){

	//Add a user element to the User vector.
	User_Data New_Entry {n, p, a};
	User.push_back(New_Entry);

	return;

}





//If I make these changes, I no longer get the errors. I don't understand why this is happening.

void New_User_Entry(string n, string p, string a, User_Data &User){

	//Add a user element to the User vector.
	User_Data_Class New_Entry {n, p, a};
	User.push_back(New_Entry);

	return;

}

this is simple but your using/typedef has masked the issue a little, making it hard to spot:

on line 69, new_entry is a vector.
on line 85, its your class.
vector does not have a constructor for 3 strings.
your class does.

you can construct your vectors with parameters, but I believe you have to do it with some objects, not 'empty'. the vector you provide is 'empty'. if you wanted 10 elements, you can say (hope I get this right)
User_Data newentry(10, {a,b,c}); //its something like this. I don't think you want this here, but if you need it later you can look up the exact syntax if I choked on it. There is a time and a place for this I guess, but generally you would think your default ctor was correct for bulk setting new entries to the same value, and that you need to override those with real data eventually. Or push back, as you do, each one unique from the start.
note that you can pushback without copying.
user.push_back (user_data_class{n,p,a}); //here again not 100% sure on the syntax, forgive me, I don't do this kind of thing a lot.
Last edited on
You can't specify a size and also allocate different data to some/all of the elements.

You can do:

1
2
3
std::vector<int> vi {1, 2, 3};	// 3 elements of 1 2 3
std::vector<int> vi1(3);	// 3 elements default initialised
std::vector<int> vi2(3, 1);	// 3 elements each initialised to 1 


To push and create a class using a constructor, it's simply:

 
user.emplace_back(n, p, a);


Consider:

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
#include <string>
#include <vector>
using namespace std;

//Classes:
class User_Data_Class {
private:
	string Name;
	string Pass;
	string Access;

public:
	User_Data_Class() {}
	User_Data_Class(const string& n, const string& p, const string& a) : Name(n), Pass(p), Access(a) {}

	string Get_Name() const { return Name; }
	string Get_Pass() const { return Pass; }
	string Get_Access() const { return Access; }
};

//Type Aliases:
using User_Data = vector<User_Data_Class>;

//Function Prototypes:
void Provide_User_Access(User_Data&);
void New_User_Entry(const string&, const string&, const string&, User_Data&);

int main() {
	User_Data Users;

	Provide_User_Access(Users);
}

void Provide_User_Access(User_Data& Users) {
	//Users to be added.
	New_User_Entry("c", "c123", "creator", Users);
}

void New_User_Entry(const string& n, const string& p, const string& a, User_Data& Users) {
	//Add a user element to the User vector.
	Users.emplace_back(n, p, a);
}

Last edited on
Not sure I follow what you're saying. The User vector is in fact empty when it's first declared. It's a vector that's expecting elements of the User_Data_Class class that I created. Later in the code, User_Data_Class class objects are added via push_back().


on line 69, new_entry is a vector.
on line 85, its your class.

I think this was my problem. I was confused with what was what. The code works very well now.


note that you can pushback without copying.

I'll look into that. Thanks.
Our posts crossed - see mine above.
Awesome. Thanks!
yes do what Seeplus said, emplace back. I always forget the name of it. There is a way to do it similar to how I said to, but IIRC it makes a hidden temp object, so that was not ideal.
Topic archived. No new replies allowed.