2012-06-22 18 views
5

मैं ऑपरेटर ओवरलोडिंग पर काम करने की कोशिश कर रहा हूँ, मेरे हेडर फाइल के होते हैं:अपरिभाषित संदर्भ >>

#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 

और वर्ग

//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; 
} 

की परिभाषा मुख्य माध्यम से किया कॉल

है
#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; 
} 

लेकिन यह संकलन मुझे एक कंपाइलर त्रुटि दिखाता है: undefined reference to 'operator>>(std:istream&, Phonenumber&)' क्या कोई मुझे इस त्रुटि को हल करने में मदद कर सकता है

+6

मैं इनपुट धारा ऑपरेटर की परिभाषा में एक 'istraem' दिखाई दे रही है। लेकिन यह सिर्फ एक टाइपो है, है ना? –

+0

क्या आप बाएं हाथ के तरफा ऑपरेटर को परिभाषित करते हैं? अगर आप 'phonenumberObj << ostrObj' लिखते हैं तो क्या यह केवल इस ऑपरेटर को कॉल नहीं करेगा? संपादित करें: कभी भी नहीं, किसी भी तरह से दूसरी बहस को याद किया है ^^ – Paranaix

+6

कुछ लोग आपको 'नेमस्पेस std का उपयोग करके कभी भी' उपयोग करने के लिए नहीं कहेंगे। मैं अब तक नहीं जाऊंगा, मुझे लगता है कि जब तक आप इसका दायरा सीमित नहीं करेंगे तब तक ठीक है। लेकिन मुझे लगता है कि * हर कोई * इस बात से सहमत होगा कि आपको इसे शीर्षलेख में वैश्विक नामस्थान में नहीं रखना चाहिए। –

उत्तर

13

त्रुटि "अपरिभाषित संदर्भ ..." एक लिंकर त्रुटि है। आपका कोड ठीक है, लेकिन आप अपनी सभी स्रोत फ़ाइलों को अंतिम उत्पाद, Phonenumber.cpp (या जिसे आप इसे कहते हैं) में लिंक नहीं कर रहे हैं।

अपने सिस्टम पर,

 
$ 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 

सूचना कैसे Phonenumber.cpp संकलन में शामिल नहीं है। आप इसे शामिल करते हैं,

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

बस एक .cpp फ़ाइल को परिभाषित नहीं पर्याप्त है, तो आप जब जोड़ने शामिल करने के लिए की है। यह हेडर फ़ाइलों पर लागू नहीं होता है।

आरेख:

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

Phonenumber.cpp ----+ 
        |---> Phonenumber.o ---+ 
       +---+      | 
       |       | 
Phonenumber.h --+       +--> a.out 
       |       | 
       +---+      | 
        |---> main.o ----------+ 
main.cpp -----------+ 
+4

स्टैक ओवरफ्लो में वास्तव में वोटिंग द्वारा सम्मानित किया जाने वाला 'ascii art master' बैज होना चाहिए :) – unkulunkulu

+0

लेकिन मैं फ़ाइल को कैसे लिंक कर सकता हूं geany का उपयोग .... –

+0

मुझे नहीं पता कि भूगर्भ क्या है। एक अलग सवाल पूछें। –