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::ostream;
//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 3 11813
blueblueblue2005 wrote: 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,
--
Ney André de Mello Zunino
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.
Ney André de Mello Zunino wrote: blueblueblue2005 wrote:
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, -- Ney André de Mello Zunino
On 24 Jun 2005 19:03:18 -0700, "blueblueblue2005" <zh******@gmail.com>
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.
--
Bob Hairgrove No**********@Home.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Steven T. Hatton |
last post by:
Scroll to the bottom and read the last part first.
I've been trying very diligently to 'modularize' the code from TC++PL3E
found here: http://www.research.att.com/~bs/matrix.c
I keep getting...
|
by: PCHOME |
last post by:
Hi!
Would someone please help me thess C error(in gcc on Linux)?
The compiler continues to give me:
readLP.o: In function `Input_Problem':
readLP.o(.text+0x0): multiple definition of...
|
by: siroregano |
last post by:
Hello-
I've got a nice C program written that uses libsndfile (#include
<sndfile.h>) to convert my raw data into a properly-formatted wav file.
The program is composed of a single .c file that...
|
by: Andrix |
last post by:
Hi!
I'm writing a class that have a static function
class Servicios {
public:
static String * intToString(int value);
};
an another class String that I write to.
|
by: pavan734 |
last post by:
Hello,
Please excuse me as Iam not posting this to correct group. I
have a parser code obtained from flex command. I have many other files.
When I compile them Iam getting a message like:...
|
by: druberego |
last post by:
I read google and tried to find the solution myself. YES I do know that
you can get undefined references if you:
a) forget to implement the code for a prototype/header file item, or
b) you forget...
|
by: zapr |
last post by:
I am trying to compile on Mac OS X 10.4 my C program. I am stuck at the
link phase where I get (gcc 4.01):
$ gcc -s -Os -Wall -fprofile-arcs -fpack-struct -fno-common -ffast-math
-ffloat-store...
|
by: wdh3rd |
last post by:
I'm still new at C and can't solve this problem. I've looked through
the FAQ and on the Web, but am not having luck.
I'm getting an "undefined reference" error as well as a "Id returned 1
exit...
|
by: spectrumdt |
last post by:
Hello.
I am trying to extend Python with some C code. I made a trivial
"Hello World" program in C that I am trying to wrap in "boilerplate"
for inclusion in a Python program. But I can't compile...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |