473,785 Members | 2,641 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

undefined reference to main and other functions

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(.tex t+0x4f): In function `main':
: undefined reference to `operator>>(std ::basic_istream <char,
std::char_trait s<char> >&, PhoneNumber&)'
/tmp/ccJDZHAY.o(.tex t+0x76): In function `main':
: undefined reference to `operator<<(std ::basic_ostream <char,
std::char_trait s<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 "PhoneNumbe r.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 "PhoneNumbe r.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

Jul 23 '05 #1
3 11837
blueblueblue200 5 wrote:
g++ fig11_05.cpp), I got error message like this:

/tmp/ccJDZHAY.o(.tex t+0x4f): In function `main':
: undefined reference to `operator>>(std ::basic_istream <char,
std::char_trait s<char> >&, PhoneNumber&)'
/tmp/ccJDZHAY.o(.tex t+0x76): In function `main':
: undefined reference to `operator<<(std ::basic_ostream <char,
std::char_trait s<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
Jul 23 '05 #2
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:
blueblueblue200 5 wrote:
g++ fig11_05.cpp), I got error message like this:

/tmp/ccJDZHAY.o(.tex t+0x4f): In function `main':
: undefined reference to `operator>>(std ::basic_istream <char,
std::char_trait s<char> >&, PhoneNumber&)'
/tmp/ccJDZHAY.o(.tex t+0x76): In function `main':
: undefined reference to `operator<<(std ::basic_ostream <char,
std::char_trait s<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


Jul 23 '05 #3
On 24 Jun 2005 19:03:18 -0700, "blueblueblue20 05" <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**********@Ho me.com
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
7770
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 what I believe are linker errors. For example: g++ -g -O2 -o rematrix cslice_iter.o main.o matrix.o rematrix.o slice_iter.o rematrix.o(.text+0x29b): In function `f(int, int)':
10
5502
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 `Input_Problem' main.o(.text+0x2f2): first defined here /usr/bin/ld: Warning: size of symbol `Input_Problem' changed from 172 to 185 in
10
21110
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 compiles without error under gnu/linux. Unfortunately, when ld tries to link the file, I get the following: bash> gcc -Wall -D_GNU_SOURCE wavconvert.c -o wavconvert
4
5729
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.
8
18053
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: undefined symbol yylex. What might have went wrong?
5
11361
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 to pass all the necessary object files to the linker. Neither of those are my problem. Please bear with me as the question I ask is rather long and I think it's beyond a CS101 level of linker stupidity. If it is a stupid CS101 mistake I'm making...
2
4370
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 -fno-strict-aliasing -fno-align-functions -fno-align-labels -fno-align-loops -fno-align-jumps -fsingle-precision-constant -nostdlib -nodefaultlibs -nostartfiles main.o -lpthread /Library/Frameworks/SDL.framework/Versions/A/SDL...
8
5627
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 status" error. I've pared down the code to a simple example: ---------------------------------square.c ---------------------------------------------------
4
5424
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 the C code. The C compiler cannot find the required function `Py_BuildValue'. My C code looks like this:
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10148
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3646
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.