Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined reference to operator >>

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

like image 206
Avinash Gopal Avatar asked Jun 22 '12 06:06

Avinash Gopal


People also ask

How to solve undefined reference error in c++?

So when we try to assign it a value in the main function, the linker doesn't find the symbol and may result in an “unresolved external symbol” or “undefined reference”. The way to fix this error is to explicitly scope the variable using '::' outside the main before using it.

What is undefined symbol error in c++?

A symbol remains undefined when a symbol reference in a relocatable object is never matched to a symbol definition. Similarly, if a shared object is used to create a dynamic executable and leaves an unresolved symbol definition, an undefined symbol error results.


1 Answers

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 -----------+
like image 191
Dietrich Epp Avatar answered Sep 27 '22 20:09

Dietrich Epp