gpt4 book ai didi

c++ - 未定义的运算符引用 >>

转载 作者:可可西里 更新时间:2023-11-01 18:19:03 27 4
gpt4 key购买 nike

我正在尝试处理运算符重载,我的头文件包括:

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

最佳答案

错误“undefined reference to...”是链接器错误。您的代码很好,但您没有将所有源文件链接到最终产品中,Phonenumber.cpp(或您称之为的任何内容)被遗漏了。

在我的系统上,

$ lsPhonenumber.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.outEnter number in the form (123) 456-7890:(555) 555-1234(555)555-1234

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

图表:

Source code ---compile--> Object files ---link--> ApplicationPhonenumber.cpp ----+                    |---> Phonenumber.o ---+                +---+                      |                |                          |Phonenumber.h --+                          +--> a.out                |                          |                +---+                      |                    |---> main.o ----------+main.cpp -----------+

关于c++ - 未定义的运算符引用 >>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11151421/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com