Design and implement a Name_pairs class holding name, age pairs where name is a string and age is a double. Represent that as a vector<string> called name and a vector<double> cales age member.
Provide an input operation read_names() that reads a series of names. Provide a read_ages() operation that prompts the user for an age for each name. Provide a print() operation (Not yet implemented) that prints out the name[i].age[i] pairs , one per line. in the porder determined by the name vector. provide a sort() operation - not yet implemented...

Sadly the above code is what I have attempted to put together but it does not even compile. Can anyone help me?

"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Do you come from another programming language? You should have learned about function calls and scopes before touching the class construct. Go back to the section about functions and how to define function parameters. A free resource like learncpp.com will work too.

You're trying to define (and hence declare) a member function named read_ages, and so you cannot pass arguments to the function. You could declare parameters instead, e.g.,

Code:

void read_ages(const string &name, double n) // member function

However, while this might permit your code to compile, it is still wrong because name is actually unnecessary: read_ages is supposed to "prompts the user for an age for each name", i.e., read_ages should make use of the names_vec member variables, not have a parameter. Likewise, n is actually unnecessary: read_ages should append to the ages_vec member variable (and you did do that, except that you need to do it in a loop that loops over the names). Note that you should not be calling push_back on names_vec in read_ages: that should have been done in read_names.

Originally Posted by Khufu

I’m not trying to learn about functions. I just asked for help with a class which is what I am trying to learn about.

Note that this is basic function declaration/definition versus a function call, which is why -Adrian told you that "You should have learned about function calls and scopes before touching the class construct". It has nothing to do with your defining a member function in a class: you would get a compile error making the same mistake defining a non-member function too.

All this means is that you tried to compare a string with a char (promoted to int). You should have written:

Code:

while(object.name != "e")

This should fix the error messages that follow too: they are basically telling you that they tried to match with other possible operations, but couldn't find a match.

That said, you should get rid of the age and name member variables: these should be local variables instead. You only need names_vec and ages_vec to be member variables (except maybe until you want to sort... you might need to introduce another member vector).

Last edited by laserlight; 11-07-2019 at 05:11 PM.

Originally Posted by Bjarne Stroustrup (2000-10-14)

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

The error tells you in no unspecific terms what's wrong. It just doesn't make sense that you're struggling with this while writing methods - this is a running before walking scenario. That's why I asked if you're coming from another language and jumped into this head first.