I have some code below that will take some names and ages and do some stuff with them. Eventually it will print them out. I need to change my print()
function with a global operator<<
. I saw on a different forum that <<operator
takes two parameters, but when I try it I get a "too many parameters for << operation error. Is there something I am doing wrong? I am newer to C++ and I really do not get the point of operator overloading.
#include <iostream>;
#include <string>;
#include <vector>;
#include <string.h>;
#include <fstream>;
#include <algorithm>;
using namespace::std;
class Name_Pairs{
vector<string> names;
vector<double> ages;
public:
void read_Names(/*string file*/){
ifstream stream;
string name;
//Open new file
stream.open("names.txt");
//Read file
while(getline(stream, name)){
//Push
names.push_back(name);
}
//Close
stream.close();
}
void read_Ages(){
double age;
//Prompt user for each age
for(int x = 0; x < names.size(); x++)
{
cout << "How old is " + names[x] + "? ";
cin >> age;
cout<<endl;
//Push
ages.push_back(age);
}
}
bool sortNames(){
int size = names.size();
string tName;
//Somethine went wrong
if(size < 1) return false;
//Temp
vector<string> temp = names;
vector<double> tempA = ages;
//Sort Names
sort(names.begin(), names.end());
//High on performance, but ok for small amounts of data
for (int x = 0; x < size; x++){
tName = names[x];
for (int y = 0; y < size; y++){
//If the names are the same, then swap
if (temp[y] == names[x]){
ages[x] = tempA[y];
}
}
}
}
void print(){
for(int x = 0; x < names.size(); x++){
cout << names[x] << " " << ages[x] << endl;
}
}
ostream& operator<<(ostream& out, int x){
return out << names[x] << " " << ages[x] <<endl;
}
};
Fortunately, by overloading the << operator, you can! Overloading operator<< is similar to overloading operator+ (they are both binary operators), except that the parameter types are different.
When you use a member function to overload a binary operator, the left-hand operand is the object that calls the overloaded operator. That means that the function only needs one explicit parameter, the right-hand operand.
A main benefit of operator overloading is that it allows us to seamlessly integrate a new class type into our programming environment. This type extensibility is an important part of the power of an oops languages such as c#.
Overloaded operators are functions with special names: the keyword "operator" followed by the symbol for the operator being defined. Like any other function, an overloaded operator has a return type and a parameter list.
An operator can be overloaded by defining a function to it. The function of the operator is declared by using the operator keyword. Syntax : Note : Operator overloading is basically the mechanism of providing a special meaning to an ideal C# operator w.r.t. a user-defined data type such as structures or classes.
You have declared bool operator< as a member function. Member functions have an implicit first parameter for this, so your operator really expects three parameters. You can solve this by using a non-member ( friend in your case - remember that friend functions are non-member functions):
For example, we can overload an operator ‘+’ in a class like String so that we can concatenate two strings by just using +. Other example classes where arithmetic operators may be overloaded are Complex Number, Fractional Number, Big Integer, etc. A simple and complete example.
Benefits of Operator Overloading : 1 Operator Overloading provides additional capabilities to C 2 operators when they are applied to user-defined data types. 3 Operators may be considered as functions internal to the compiler. More ...
You are overloading <<
operator as a member function, therefore, the first parameter is implicitly the calling object.
You should either overload it as friend
function or as a free function. For example:
overloading as friend
function.
friend ostream& operator<<(ostream& out, int x){
out << names[x] << " " << ages[x] <<endl;
return out;
}
However, the canonical way is to overload it as free
function. You can find very good information from this post: C++ operator overloading
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With