C++ 对操作员的未定义引用>&燃气轮机;
我正在尝试运算符重载,我的头文件包括:C++ 对操作员的未定义引用>&燃气轮机;,c++,undefined-reference,C++,Undefined Reference,我正在尝试运算符重载,我的头文件包括: #ifndef PHONENUMBER_H #define PHONENUMBER_H #include<iostream> #include<string> using namespace std; class Phonenumber { friend ostream &operator << ( ostream&, const Phonenumber & ); friend
#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
\ifndef电话号码\u H
#定义电话号码
#包括
#包括
使用名称空间std;
类电话号码
{
friend ostream&operator>(istream&,Phonenumber&);
私人:
字符串区域码;
字符串交换;
弦线;
};
#endif//PHONENUMBER\u 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;
}
//重载流插入和提取运算符
//类电话号码
#包括
#包括“Phonenumber.h”
使用名称空间std;
//重载stram插入运算符不能是成员函数
//如果我们想用
//法院(cin,电话)
cin>>手机;
//cout(cout,电话)
cout错误“未定义引用…”是链接器错误。您的代码很好,但您没有将所有源文件链接到最终产品,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
仅仅定义.cpp
文件是不够的,链接时必须包含。这不适用于头文件
图表:
Source code ---compile--> Object files ---link--> Application
Phonenumber.cpp ----+
|---> Phonenumber.o ---+
+---+ |
| |
Phonenumber.h --+ +--> a.out
| |
+---+ |
|---> main.o ----------+
main.cpp -----------+
源代码---编译-->对象文件---链接-->应用程序
Phonenumber.cpp----+
|--->电话号码---+
+---+ |
| |
Phonenumber.h-->++-->a.out
| |
+---+ |
|--->梅因----------+
main.cpp-----------+
我在输入流操作符的定义中看到了一个istraem
。但这只是一个打字错误,不是吗?你不是在定义一个左手边的操作符吗?如果你写phonenumberObj,它不会只调用这个操作符吗?有些人会告诉你永远不要使用usingnamespace-std代码>。我不会走那么远,我认为只要你限制它的范围就可以了。但我想每个人都会同意,你不应该把它放在全局名称空间的标题中。@BenjaminLindles谁这么说的?我同意你的观点,在全局空间(例如在标题中)使用它是不好的。但是,如果您在实现文件中使用它,人们为什么要关心呢?它使代码更具可读性,通常不会产生任何含糊不清的名称。如果您这样做,只需将这几个类明确地与namespace一起使用即可
fromPhonenumber.h
。stackoverflow真的应该有“ascii艺术大师”徽章,通过投票获得:)我不知道geany是什么。另问一个问题。
$ g++ main.cpp Phonenumber.cpp
$ ./a.out
Enter number in the form (123) 456-7890:
(555) 555-1234
(555)555-1234
Source code ---compile--> Object files ---link--> Application
Phonenumber.cpp ----+
|---> Phonenumber.o ---+
+---+ |
| |
Phonenumber.h --+ +--> a.out
| |
+---+ |
|---> main.o ----------+
main.cpp -----------+