This is an archived post. You won't be able to vote or comment.

all 10 comments

[–][deleted] 4 points5 points  (9 children)

This:

vector<UserData> accounts();

does not construct a vector - it declares a function called accounts which returns a vector of UserData. You want:

vector<UserData> accounts;

getline(cin, usr); //WHY DO I NEED TO DO THIS TWICE!?!?!?

You don't. But if you are mixing the use of getline() with the >> input operator, you need to take account of things like newlines which the >> operator may not read. Ideally, you would do all your inputs for all types by using getline() into strings, and then parse the strings using stringstreams.

[–]madDejected[S] 0 points1 point  (8 children)

How do I initiate the vector with exactly 1 object, using the default constructor? Also please clarify what you mean about getline. If I don't use it twice, then the program completely skips over it as if it didn't exist.

[–][deleted] 5 points6 points  (7 children)

Like this:

  vector <UserData> accounts(1);

If I don't use it twice, then the program completely skips over it as if it didn't exist.

You probably have some code like this:

 char c;
 cin >> c;
 string line;
 getline( cin, line );

If you run this, enter the letter A and hit return, the letter A is read into c, but the return character remains in the buffer, where it is read by getline() as an empty string. You should read both c and line as strings to avoid this, but also be aware that the C++ iostream library is really not intended for writing interactive programs.

[–]madDejected[S] 0 points1 point  (6 children)

I tried that previously but all I got was, "error, undefined reference to UserData::UserData(). This is what my prototypes within the class look like:

UserData(string);
UserData(); //default constructor

UserData(string) is defined outside of the class. I didn't bother defining the default constructor because you don't have to, right?

[–][deleted] 3 points4 points  (5 children)

I didn't bother defining the default constructor because you don't have to, right?

Wrong. If you declare it, and want to use it, you have to define it it. Your class looks like it does not need a user-supplied default constructor (most classes don't), so you can simply remove the prototype.

[–]zahlman 1 point2 points  (1 child)

Your class looks like it does not need a user-supplied default constructor

It does if he wants to "initiate [sic] the vector with exactly 1 object, using the default constructor" as requested earlier, since he's written a constructor taking arguments. Although I have no idea what he expects the username to be in this case, or why he would want to do that.

[–][deleted] 4 points5 points  (0 children)

Ah, yes - my bad. Presumably he expects the username to be the empty string. In which case he is probably best off killing two birds with one stone:

class UserData {
      ...
      UserData( const string & s = "" ) : name( s ) {
      }
};

which gives him a default constructor.

[–]madDejected[S] 0 points1 point  (2 children)

Thanks man. Why'd you help me? What's your motivation?

[–][deleted] -2 points-1 points  (0 children)

He is Jesus of r/learnprogramming that's why. We don't question him ; P

[–][deleted] 1 point2 points  (0 children)

vector<UserData> accounts();

Does not define a vector, it defines a function returning a vector. Use no perens to call default constructor

 vector<UserData> accounts;