Undefined reference to operator >>
Asked Answered
V

1

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

Venterea answered 22/6, 2012 at 6:50 Comment(10)
I'm seeing an istraem in the definition of the input stream operator. But it is just a typo, isn't it?Apologue
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 ^^Bray
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.Erythroblast
@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.Bray
@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.Erythroblast
you should indeed remove that using namespace std; from Phonenumber.h.Principium
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. stackoverflow.com/search?q=using+namespace+std+[c%2B%2B]Noe
the code itself, bar a typo, is fine. You are not building it properly.Daedalus
possible duplicate of "undefined reference to" in G++ CppPartner
@ Charles, benjamin , paranaix and all thanks for your suggestion....and for rest the code is correct....without ant typo error...Venterea
M
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 -----------+
Matilda answered 22/6, 2012 at 7:46 Comment(2)
stackoverflow really should have 'ascii art master' badge to be awarded by voting :)Sprue
I don't know what geany is. Ask a separate question.Matilda

© 2022 - 2024 — McMap. All rights reserved.