B
blueblueblue2005
Hi, I am learning C++ using the examples from Deitel How to Program in
C++. I download the example code. now I am working on the operator
overloading. I am working under Ubuntu Linux 5.04. I installed the g++
compiler package from synaptic package manager, g++-3.3, and set up a
soft link g++. when I compile the sample code( the command I use is:
g++ fig11_05.cpp), I got error message like this:
/tmp/ccJDZHAY.o(.text+0x4f): In function `main':
: undefined reference to `operator>>(std::basic_istream<char,
std::char_traits<char> >&, PhoneNumber&)'
/tmp/ccJDZHAY.o(.text+0x76): In function `main':
: undefined reference to `operator<<(std::basic_ostream<char,
std::char_traits<char> >&, PhoneNumber const&)'
collect2: ld returned 1 exit status
The program is a very simple one, the source code are following(there
are 3 files)
// Fig. 11.3: PhoneNumber.h
// PhoneNumber class definition
#ifndef PHONENUMBER_H
#define PHONENUMBER_H
#include <iostream>
using namespace std;
//using std:stream;
//using std::istream;
#include <string>
using std::string;
class PhoneNumber
{
friend ostream &operator<<( ostream &, const PhoneNumber & );
friend istream &operator>>( istream &, PhoneNumber & );
private:
string areaCode; // 3-digit area code
string exchange; // 3-digit exchange
string line; // 4-digit line
}; // end class PhoneNumber
#endif
#include <iomanip>
using std::setw;
#include "PhoneNumber.h"
// overloaded stream insertion operator; cannot be
// a member function if we would like to invoke it with
// cout << somePhoneNumber;
ostream &operator<<( ostream &output, const PhoneNumber &number )
{
output << "(" << number.areaCode << ") "
<< number.exchange << "-" << number.line;
return output; // enables cout << a << b << c;
} // end function operator<<
// overloaded stream extraction operator; cannot be
// a member function if we would like to invoke it with
// cin >> somePhoneNumber;
istream &operator>>( istream &input, PhoneNumber &number )
{
input.ignore(); // skip (
input >> setw( 3 ) >> number.areaCode; // input area code
input.ignore( 2 ); // skip ) and space
input >> setw( 3 ) >> number.exchange; // input exchange
input.ignore(); // skip dash (-)
input >> setw( 4 ) >> number.line; // input line
return input; // enables cin >> a >> b >> c;
} // end function operator>>
// Fig. 11.5: fig11_05.cpp
// Demonstrating class PhoneNumber's overloaded stream insertion
// and stream extraction operators.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include "PhoneNumber.h"
int main()
{
PhoneNumber phone; // create object phone
cout << "Enter phone number in the form (123) 456-7890:" << endl;
// cin >> phone invokes operator>> by implicitly issuing
// the global function call operator>>( cin, phone )
cin >> phone;
cout << "The phone number entered was: ";
// cout << phone invokes operator<< by implicitly issuing
// the global function call operator<<( cout, phone )
cout << phone << endl;
return 0;
} // end main
C++. I download the example code. now I am working on the operator
overloading. I am working under Ubuntu Linux 5.04. I installed the g++
compiler package from synaptic package manager, g++-3.3, and set up a
soft link g++. when I compile the sample code( the command I use is:
g++ fig11_05.cpp), I got error message like this:
/tmp/ccJDZHAY.o(.text+0x4f): In function `main':
: undefined reference to `operator>>(std::basic_istream<char,
std::char_traits<char> >&, PhoneNumber&)'
/tmp/ccJDZHAY.o(.text+0x76): In function `main':
: undefined reference to `operator<<(std::basic_ostream<char,
std::char_traits<char> >&, PhoneNumber const&)'
collect2: ld returned 1 exit status
The program is a very simple one, the source code are following(there
are 3 files)
// Fig. 11.3: PhoneNumber.h
// PhoneNumber class definition
#ifndef PHONENUMBER_H
#define PHONENUMBER_H
#include <iostream>
using namespace std;
//using std:stream;
//using std::istream;
#include <string>
using std::string;
class PhoneNumber
{
friend ostream &operator<<( ostream &, const PhoneNumber & );
friend istream &operator>>( istream &, PhoneNumber & );
private:
string areaCode; // 3-digit area code
string exchange; // 3-digit exchange
string line; // 4-digit line
}; // end class PhoneNumber
#endif
#include <iomanip>
using std::setw;
#include "PhoneNumber.h"
// overloaded stream insertion operator; cannot be
// a member function if we would like to invoke it with
// cout << somePhoneNumber;
ostream &operator<<( ostream &output, const PhoneNumber &number )
{
output << "(" << number.areaCode << ") "
<< number.exchange << "-" << number.line;
return output; // enables cout << a << b << c;
} // end function operator<<
// overloaded stream extraction operator; cannot be
// a member function if we would like to invoke it with
// cin >> somePhoneNumber;
istream &operator>>( istream &input, PhoneNumber &number )
{
input.ignore(); // skip (
input >> setw( 3 ) >> number.areaCode; // input area code
input.ignore( 2 ); // skip ) and space
input >> setw( 3 ) >> number.exchange; // input exchange
input.ignore(); // skip dash (-)
input >> setw( 4 ) >> number.line; // input line
return input; // enables cin >> a >> b >> c;
} // end function operator>>
// Fig. 11.5: fig11_05.cpp
// Demonstrating class PhoneNumber's overloaded stream insertion
// and stream extraction operators.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include "PhoneNumber.h"
int main()
{
PhoneNumber phone; // create object phone
cout << "Enter phone number in the form (123) 456-7890:" << endl;
// cin >> phone invokes operator>> by implicitly issuing
// the global function call operator>>( cin, phone )
cin >> phone;
cout << "The phone number entered was: ";
// cout << phone invokes operator<< by implicitly issuing
// the global function call operator<<( cout, phone )
cout << phone << endl;
return 0;
} // end main