【问题标题】:Undefined reference to operator >>对运算符的未定义引用 >>
【发布时间】:2012-06-24 11:17:35
【问题描述】:

我正在尝试操作符重载,我的头文件包括:

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

通过 main 调用是

#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&gt;&gt;(std:istream&amp;, Phonenumber&amp;)' 有人可以帮我解决这个错误

【问题讨论】:

  • 我在输入流运算符的定义中看到了一个istraem。但这只是一个错字,不是吗?
  • 你不是在定义一个左手操作符吗?如果你写phonenumberObj &lt;&lt; ostrObj,它不会只调用这个运算符吗?编辑:没关系,不知何故错过了第二次争论^^
  • 有些人会告诉你永远不要使用using namespace std;。我不会走那么远,我认为只要你限制它的范围就可以了。但我认为每个人都会同意你不应该把它放在头文件的全局命名空间中。
  • @BenjaminLindles 谁说的?我同意你的观点,在全局空间(例如在标题中)使用它是不好的。但是,为什么要关心您是否在实现文件中使用它呢?它使代码更具可读性,通常你不会用它产生任何模棱两可的名字。如果你这样做了,只需在命名空间中显式地使用这几个类。
  • 您确实应该从Phonenumber.h 中删除using namespace std;

标签: c++ undefined-reference


【解决方案1】:

错误“未定义对...的引用”是链接器错误。您的代码很好,但您并未将所有源文件链接到最终产品中,Phonenumber.cpp(或您所称的任何名称)被忽略了。

在我的系统上,

$ ls 电话号码.cpp 电话号码.h main.cpp $ g++ main.cpp /tmp/cce0OaNt.o:在函数“主”中: main.cpp:(.text+0x40): undefined reference to `operator>>(std::basic_istream >&, Phonenumber&)' main.cpp:(.text+0x51): undefined reference to `operator >&, Phonenumber const&)' collect2: ld 返回 1 个退出状态

注意Phonenumber.cpp 是如何不包含在编译中的。如果包含它,

$ g++ main.cpp Phonenumber.cpp $ ./a.out 在 (123) 456-7890 表格中输入号码: (555) 555-1234 (555)555-1234

仅仅定义一个.cpp 文件是不够的,你必须在链接时包含。这不适用于头文件。

图表:

源代码---编译--->目标文件---链接--->应用 电话号码.cpp -+ |---> 电话号码.o ---+ +---+ | | | Phonenumber.h --+ +--> a.out | | +---+ | |---> main.o ----------+ main.cpp -----------+

【讨论】:

  • stackoverflow 真的应该有“ascii 艺术大师”徽章,可以通过投票获得:)
  • 我不知道 geany 是什么。问一个单独的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多