undefined reference to main and other functions

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::eek: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
 
?

=?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=

blueblueblue2005 said:
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)

And yet you are only providing the name of one of them to your compiler.
No wonder why your linker is unable to resolve the references. Try it
again with a proper command line; something like:

g++ -o <output-file> <source-file-1> <source-file-2> ...

One more thing, it would have been easier if you had started out with
the basic "Hello, world!", as most everybody else, before moving on.

Hope this helps you,
 
B

blueblueblue2005

Thanks very much :), I thought g++ will automatically link the included
file as java does. so if there are a lot source cpp file, I better
write a make file.
 
B

Bob Hairgrove

On 24 Jun 2005 19:03:18 -0700, "blueblueblue2005" <[email protected]>
wrote:

What everyone else said, but watch out here:

[snipped some code...]
int main()
{
PhoneNumber phone; // create object phone
cout << "Enter phone number in the form (123) 456-7890:" << endl;

Since you have white space in the input, cin will not work as
expected. Check out the getline function instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top