重载运算符

时间:2017-01-19 19:30:59

标签: xcode linker g++ overloading operator-keyword

当我尝试从C ++编译示例程序时,我不断收到以下错误:Deitel和Deitel。我用 g ++ Fig11_05.cpp -o Fig11_05

我花了好几个小时试图通过查找互联网来解决这个问题,特别是stackoverflow,但没有用!

我尝试使用不同的命令行参数,例如-libstd = libc ++, - std = c ++ 11,-std = c ++ 14

我一直得到的错误是这样的: 架构x86_64的未定义符号:   "运算符<<(std :: __ 1 :: basic_ostream>&,PhoneNumber const&)",引自:       _11在图11_05-1f04bd.o中   "运算符>>(std :: __ 1 :: basic_istream>&,PhoneNumber&)",引自:       _11在图11_05-1f04bd.o中 ld:找不到架构x86_64的符号 clang:错误:链接器命令失败,退出代码为1(使用-v查看调用)

g ++ -v 的结果:

配置: - prefix = / Applications / Xcode.app / Contents / Developer / usr --with-gxx-include-dir = / Applications / Xcode.app / Contents / Developer / Platforms / MacOSX.platform / Developer /SDKs/MacOSX10.12.sdk/usr/include/c++/4.2.1 Apple LLVM 8.0.0版(clang-800.0.42.1) 目标:x86_64-apple-darwin15.6.0 线程模型:posix InstalledDir:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

代码:

// Fig. 11.3: PhoneNumber.h                                                                                                                                   
// PhoneNumber class definition                                                                                                                               
#ifndef PHONENUMBER_H
#define PHONENUMBER_H

#include <iostream>
#include <string>


class PhoneNumber
{
  friend std::ostream& operator<<(std::ostream&, const PhoneNumber&);
  friend std::istream &operator>>(std::istream&, PhoneNumber&);
private:
  std::string areaCode; // 3-digit area code                                                                                                                  
  std::string exchange; // 3-digit exchange                                                                                                                   
  std::string line; // 4-digit line                                                                                                                           
}; // end class PhoneNumber                                                                                                                                   

#endif

// Fig. 11.4: PhoneNumber.cpp                                                                                                                                 
// Overloaded stream insertion and stream extraction operators                                                                                                
// for class PhoneNumber.                                                                                                                                     
#include <iomanip>
#include "PhoneNumber.h"
using namespace std;

// overloaded stream 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 << "Area Code: "  << number.areaCode << "\nExchange: "
          << number.exchange << "\nLine: " << number.line << "\n"
          << "(" << number.areaCode << ") " << number.exchange << "-"
          << number.line << "\n";;
   return output; // enables cout << a << b << c;                                                                                                             
} // end function operator<<                                                                                                                                  

// overloaded stream extraction operator; cannot be                                                                                                           
// a member function if we would like to invoke it with                                                                                                       
// cin >> somePhoneNumber;                                                                                                                                    
istream& operator>>(istream& input, PhoneNumber& number)
{
   input.ignore(); // skip (                                                                                                                                  
   input >> setw(3) >> number.areaCode; // input area code                                                                                                    
   input.ignore(2); // skip ) and space                                                                                                                       
   input >> setw(3) >> number.exchange; // input exchange                                                                                                     
   input.ignore(); // skip dash (-)                                                                                                                           
   input >> setw(4) >> number.line; // input line                                                                                                             
   return input; // enables cin >> a >> b >> c;                                                                                                               
} // end function operator>>   


// Fig. 11.5: fig11_05.cpp                                                                                                                                    
// Demonstrating class PhoneNumber's overloaded stream insertion                                                                                              
// and stream extraction operators.                                                                                                                           
#include <iostream>
#include "PhoneNumber.h"
using namespace std;

int main()
{
   PhoneNumber phone; // create object phone                                                                                                                  

   cout << "Enter phone number in the form (123) 456-7890:" << endl;

   // cin >> phone invokes operator>> by implicitly issuing                                                                                                   
   // the global function call operator>>( cin, phone )                                                                                                       
   cin >> phone;

   cout << "\nThe phone number entered was:\n";

   // cout << phone invokes operator<< by implicitly issuing                                                                                                  
   // the global function call operator<<( cout, phone )                                                                                                      
   cout << phone << endl;
} // end main       

注意:我最近安装了然后卸载了CUDA工具包8.我需要更新版本的Xcode,所以我安装了Xcode最新版本8.2.1,并将旧版本保存在不同的目录中如果。我不认为问题在于安装Xcode。另外,当我安装CUDA时,我必须将DYLD_LIBRARY_PATH设置为目录。但是,它可能不是问题的根源!我只是想帮你解决问题,以帮助我解决它:)

提前谢谢!你是一个很好的社区!

1 个答案:

答案 0 :(得分:0)

您尝试构建该程序:

g++ Fig11_05.cpp -o Fig11_05

是不成功的,因为你忽略了编译并链接到 编写包含函数定义的代码:

std::ostream& operator<<(std::ostream& output, const PhoneNumber& number)

std::istream& operator>>(std::istream& input, PhoneNumber& number)

由您的程序调用。这就是链接器说的原因:

Undefined symbols for architecture x86_64: 
"operator<<(std::__1::basic_ostream >&, PhoneNumber const&)", referenced from: _main in Fig11_05-1f04bd.o 
"operator>>(std::__1::basic_istream >&, PhoneNumber&)", referenced from: _main in Fig11_05-1f04bd.o 
...

请改为:

g++ Fig11_05.cpp PhoneNumber.cpp -o Fig11_05

或完整拼写整个过程: -

将源文件Fig11_05.cpp编译为目标文件Fig11_05.o

$ g++ -o Fig11_05.o -c Fig11_05.cpp

将源文件PhoneNumber.cpp编译为目标文件PhoneNumber.o

$ g++ -o PhoneNumber.o -c PhoneNumber.cpp

将对象文件Fig11_05.oPhoneNumber.o链接到程序Fig11_05

$ g++ -o Fig11_05 Fig11_05.o PhoneNumber.o

然后您可以运行:

./Fig11_05

这是a fairly good beginner's tutorial 关于用GCC构建C或C ++程序。