2012-06-22 18 views
5

, benim başlık dosyasını oluşur: ana aracılığıyla yapılır görüşmesi özelliğiniTanımsız referans >> Ben operatör aşırı yüklenme üzerinde çalışmaya çalışıyorum

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

ait

#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 

Ve sınıf tanımının

olduğunu
#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; 
} 

fakat bunun derlenmesi bana bir derleyici hatası gösterir: undefined reference to 'operator>>(std:istream&, Phonenumber&)' Birisi bu hatayı çözmemde bana yardımcı olabilir

+6

. Ama bu sadece bir yazım hatası değil mi? –

+0

Sol taraftaki bir operatörü tanımladığınızdan emin misiniz? Eğer "fonemumberObj << ostrObj" yazıyorsanız sadece bu operatöre çağrı mı? Düzenleyin: Nevermind, bir şekilde ikinci tartışmayı kaçırdınız ^^ – Paranaix

+6

Bazı insanlar asla 'namespace std;' kullanarak kullanmamayı söyleyecektir. O kadar uzağa gitmezdim, sanırım kapsamını sınırlandırdığınız sürece sorun yok. Ama bence * herkes * bir başlıktaki küresel isim alanına koymamalısınız. –

cevap

13

"Tanımlanmamış referans ..." hatası bir linker hatasıdır. Kodunuz gayet iyi, ancak tüm kaynak dosyalarınızı son ürüne bağlamazsınız, Phonenumber.cpp (ya da her ne iseniz) dışarıda kalıyor. Benim sistemde

, Phonenumber.cpp derleme dahil değildir nasıl

 
$ 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 

dikkat edin. Eğer eklerseniz

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

Basitçe bir .cpp dosyayı tanımlayan yeterli değildir, sen bağlarken dahil etmek gerekir. Bu başlık dosyaları için geçerli değildir.

Şema: I girdi akımı operatörünün tanımındaki bir `istraem` görüyorum

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

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

stackoverflow gerçekten oy 'ascii sanat ustası' rozeti olmalı :) – unkulunkulu

+0

ama nasıl dosyayı bağlayabilirsiniz geany kullanarak ... –

+0

Ben geany ne olduğunu bilmiyorum. Ayrı bir soru sorun. –

İlgili konular