Good day,
I'm having bit of an issue regarding writing to and printing from a user defined type.
Here is the code that doesn't work.
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
|
#include "stdafx.h"
#include "std_lib_facilities.h"
class name_value
{
public:
string name;
double score;
name_value()
:name(), score(0) {}
name_value(string name, double score)
:name(name), score(score) {}
};
int main()
{
cout << "Please enter a name and score\n";
cout << "Be sure to put a space between the name and score\n";
name_value name_score;
cout << ">: ";
cin >> name_score;
cout << '\n' << name_score << '\n';
keep_window_open();
return 0;
}
| |
Now here is the code that does.
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
|
#include "stdafx.h"
#include "std_lib_facilities.h"
class name_value
{
public:
string name;
double score;
name_value()
:name(), score(0) {}
name_value(string name, double score)
:name(name), score(score) {}
};
int main()
{
cout << "Please enter a name and score\n";
cout << "Be sure to put a space between the name and score\n";
name_value name_score;
cout << ">: ";
cin >> name_score.name >> name_score.score;
cout << '\n' << name_score.name << " " << name_score.score << '\n';
keep_window_open();
return 0;
}
| |
The issue is that this is just the first step in building a bit more complex program.
Using the class "name_value", I'll need to make a vector with it.
vector<name_value>name_score_vector;
I then need to be able to print out each name_score within the vector on separate lines. Before that, I need to compare name_score to what already in the vector to make sure the same name is not repeated twice.
The error I get is;
E0349 no operator ">>" matches these operands
C2679 binary '>>': no operator found which takes a right-hand operand of type 'name_value' (or there is no acceptable conversion)
Is there something I can add to the type definition so ">>" and "<<" are recognized simply using "name_class" and not "name_class.name" or "name_class.value"?
Thank you.