Reputation: 11
I'm studying C++ with Deitel's book. I'm trying to compile operator overloading example, but it fails. Can you explain me why? Here is the code:
Class prototype:
// PhoneNumber class definition
#include <iostream>
#include <string>
#ifndef PHONENUMBER_H
#define PHONENUMBER_H
class PhoneNumber
{ // Start class PhoneNumber
friend std::ostream & operator<<( std::ostream &output, const PhoneNumber &number );
friend std::istream & operator>>( std::istream &input, PhoneNumber &number);
private:
// 3-digit area code
std::string areaCode;
// 3-digit exchange
std::string exchange;
// 4-digit line
std::string line;
}; // End Class PhoneNumber
#endif // PHONENUMBER_H
Class definition:
// Overloaded stream insertion and stream extraction operators
// for class PhoneNumber
#include <iomanip>
#include "phonenumber.h"
using namespace std;
// overloaded stream insertion operator; can't be
// a member function if we would like to invoke it with
// cout << somePhoneNumber;
ostream & operator <<( ostream &output, const PhoneNumber &number )
{ // Start operator<<()
output << "(" << number.areaCode << ") " << number.exchange < "-" << number.line;
return output; // enables cout << a << b << c;
} // End operator<<()
// overloaded stream extraction operator; can't be
// a member function if we would like to invoke it with
// cin >> somePhoneNumber
istream & operator >>( istream &input, PhoneNumber &number )
{ // Start operator>>()
// skip (
input.ignore();
// input area code
input >> setw( 3 ) >> number.areaCode;
// skip ) and space
input.ignore( 2 );
// input exchange
input >> setw( 3 ) >> number.exchange;
// skip dash(-)
input.ignore();
// input line
input >> setw( 4 ) >> number.line;
return input; // enables cin >> a >> b >> c;
} // end operator>>()
Test function:
// Demonstrating class PhoneNumber's overloaded stream insertion
// and stream extraction operators
#include <iostream>
#include "phonenumber.h"
using namespace std;
int main(int argc, char *argv[])
{
PhoneNumber phone; // create object phone
cout << "Enter phone number in the form (123) 456-7890:" << endl;
// cin >> phone invokes operator>> by implicitly issuing
// the non-member function call operator>>(cin, phone )
cin >> phone;
cout << "The phone number entered was: ";
// cout << phone invokes operator<< by implicitly issuing
// the non-member function call operator<<( cout, phone)
cout << phone << endl;
return 0;
}
When I try to build it I get an error message:
phonenumber.cpp:13: error: no match for ‘operator<<’ (operand types are ‘const char [2]’ and ‘const string {aka const std::__cxx11::basic_string<char>}’)
output << "(" << number.areaCode << ") " << number.exchange < "-" << number.line;
Can you explain me please what is the reason of the error?
Upvotes: 1
Views: 1271
Reputation: 2313
You have a simple typo on the line:
output << "(" << number.areaCode << ") " << number.exchange < "-" << number.line;
Specifically:
number.exchange < "-"
Should be:
number.exchange << "-"
The compiler believes that you are trying to use the <
operator on the two strings. Simply fix the line and it works.
The error message given to me on my system was:
main.cpp:30:71: error: no match for 'operator<<' (operand types are 'const char [2]' and 'const string {aka const std::__cxx11::basic_string<char>}')
output << "(" << number.areaCode << ") " << number.exchange < "-" << number.line;
~~~~^~~~~~~~~~~~~~
Upvotes: 2