8

I am trying to work on operator overloading, my header file consists of:

#ifndef PHONENUMBER_H
#define PHONENUMBER_H

#include<iostream>
#include<string>
using namespace std;

class Phonenumber
{
    friend ostream &operator << ( ostream&, const Phonenumber & );
    friend istream &operator >> ( istream&, Phonenumber & );
private:
    string areaCode;
    string exchange;
    string line;

};

#endif // PHONENUMBER_H

And class definition of

//overload stream insertion and extraction operators
//for class Phonenumber
#include <iomanip>
#include "Phonenumber.h"
using namespace std;
//overloades stram 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;

}//end function opertaor <<

istream &operator >> ( istream &input, Phonenumber &number)
{
    input.ignore(); //skip (
    input>>setw(3)>>number.areaCode;//input areacode
    input.ignore(2);//skip ) and space
    input>>setw(3)>>number.exchange;//input exchange
    input.ignore();//skip -
    input>>setw(4)>>number.line;//input line
    return input;
}

calling done through main is

#include <iostream>
#include"Phonenumber.h"
using namespace std;

int main()
{
    Phonenumber phone;
    cout<<"Enter 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<< phone invokes operator << by implicitly issuing the non-member function call operator>>(cout,phone)
    cout << phone<<endl;
    return 0;
}

but compiling this shows me a compiler error: undefined reference to 'operator>>(std:istream&, Phonenumber&)' Could someone help me to resolve this error

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
Avinash Gopal
  • 101
  • 1
  • 1
  • 5
  • 6
    I'm seeing an `istraem` in the definition of the input stream operator. But it is just a typo, isn't it? – Luca Geretti Jun 22 '12 at 06:53
  • Arent you defining a left hand sided operator? Wouldnt it only call this operator if you write `phonenumberObj << ostrObj`? Edit: Nevermind, have somehow missed the second arguement ^^ – Sebastian Hoffmann Jun 22 '12 at 06:55
  • 6
    Some people will tell you to never use `using namespace std;`. I wouldn't go that far, I think it's okay as long as you limit its scope. But I think *everyone* will agree that you shouldn't put it in the global namespace in a header. – Benjamin Lindley Jun 22 '12 at 07:02
  • @BenjaminLindles Who says that? I agree with you that using it at global space (e.g in a header) is bad. But why should one ever care if your using it in your implementation files? It makes code much more readable and normally you wont produce any ambigious names with it. If you do though, simply use these few classes explicitely with namespace. – Sebastian Hoffmann Jun 22 '12 at 07:17
  • @Paranaix: Ask a few questions in a busier time of day with `using namespace std;` in them, and you'll find out who says that. Not I. – Benjamin Lindley Jun 22 '12 at 07:22
  • 2
    you should indeed remove that `using namespace std;` from `Phonenumber.h`. – moooeeeep Jun 22 '12 at 07:29
  • Don't use `using namespace std;`. It's not helpful for beginners; it's not helpful in large projects; it has very limited legitimate uses. http://stackoverflow.com/search?q=using+namespace+std+[c%2B%2B] – CB Bailey Jun 22 '12 at 07:48
  • the code itself, bar a typo, is fine. You are not building it properly. – juanchopanza Jun 22 '12 at 07:48
  • possible duplicate of ["undefined reference to" in G++ Cpp](http://stackoverflow.com/questions/6978241/undefined-reference-to-in-g-cpp) – Bo Persson Jun 22 '12 at 09:51
  • @ Charles, benjamin , paranaix and all thanks for your suggestion....and for rest the code is correct....without ant typo error... – Avinash Gopal Jun 27 '12 at 08:51

1 Answers1

16

The error "undefined reference to..." is a linker error. Your code is fine, but you are not linking all of your source files into the final product, Phonenumber.cpp (or whatever you call it) is being left out.

On my system,

$ ls
Phonenumber.cpp  Phonenumber.h  main.cpp
$ g++ main.cpp
/tmp/cce0OaNt.o: In function `main':
main.cpp:(.text+0x40): undefined reference to `operator>>(std::basic_istream<char, std::char_traits<char> >&, Phonenumber&)'
main.cpp:(.text+0x51): undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Phonenumber const&)'
collect2: ld returned 1 exit status

Notice how Phonenumber.cpp is not included in the compilation. If you include it,

$ g++ main.cpp Phonenumber.cpp
$ ./a.out
Enter number in the form (123) 456-7890:
(555) 555-1234
(555)555-1234

Simply defining a .cpp file is not enough, you have to include when linking. This does not apply to header files.

Diagram:

Source code ---compile--> Object files ---link--> Application

Phonenumber.cpp ----+
                    |---> Phonenumber.o ---+
                +---+                      |
                |                          |
Phonenumber.h --+                          +--> a.out
                |                          |
                +---+                      |
                    |---> main.o ----------+
main.cpp -----------+
Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415