2012-06-22 10 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'を書くなら、それはこの演算子を呼ぶでしょうか?編集:気にしないで、何とか2番目の引数を忘れてしまった^^ – Paranaix

+6

'namespace std; 'を使わないように言う人もいます。私はそれほど遠くに行かないだろう、あなたがその範囲を制限する限り、それは大丈夫だと思う。しかし、私は誰も*あなたがヘッダのグローバルな名前空間に入れてはならないと同意すると思います。 –

答えて

13

"未定義の参照は..."というエラーはリンカーエラーです。あなたのコードは問題ありませんが、ソースファイルのすべてを最終製品のPhonenumber.cpp(またはあなたが呼んでいるもの)にリンクしているわけではありません。私のシステムで

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 

注意してください。あなたはそれを含めると、

 
$ 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

stackoverflowは本当に 'asciiアートマスター'バッジが投票によって授与される必要があります:) – unkulunkulu

+0

しかし、私はファイルをリンクすることができますgeanyを使って.... –

+0

私はgeanyが何であるかわかりません。別の質問をしてください。 –

関連する問題