I am trying to work with operator overloading, my header file consists of:
#ifndef PHONENUMBER_H
#define PHONENUMBER_H
#include<iostream>
#include<string>
using namespace std;
class Phonenumber
{
friend ostream &operator << ( ostream&, const Phonenumber & );
friend istream &operator >> ( istream&, Phonenumber & );
private:
string areaCode;
string exchange;
string line;
};
#endif
And class definition
#include <iomanip>
#include "Phonenumber.h"
using namespace std;
ostream &operator << ( ostream &output, const Phonenumber &number)
{
output<<"("<<number.areaCode<<")"
<<number.exchange<<"-"<<number.line;
return output;
}
istream &operator >> ( istream &input, Phonenumber &number)
{
input.ignore();
input>>setw(3)>>number.areaCode;
input.ignore(2);
input>>setw(3)>>number.exchange;
input.ignore();
input>>setw(4)>>number.line;
return input;
}
a call made through main,
#include <iostream>
#include"Phonenumber.h"
using namespace std;
int main()
{
Phonenumber phone;
cout<<"Enter number in the form (123) 456-7890:"<<endl;
cin >> phone;
cout << phone<<endl;
return 0;
}
but compilation shows me a compiler error: undefined reference to 'operator>>(std:istream&, Phonenumber&)'
Can someone help me solve this error?
source
share