m***@gmail.com
2017-01-20 12:47:58 UTC
I keep getting the following error when I try to compile an example program from C++ how to program, Deitel and Deitel. I used
g++ Fig11_05.cpp -o Fig11_05
I spent hours trying to solve this problem by looking up the Internet and particularly stackoverflow, but with no avail!
I tried using different command line arguments such as -libstd=libc++, -std=c++11, -std=c++14
the error I keep getting is as this:
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 ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
The result of g++ -v:
Configured with: --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 version 8.0.0 (clang-800.0.42.1) Target: x86_64-apple-darwin15.6.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
The codes:
// 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
Note: I recently installed then uninstalled CUDA toolkit 8. I needed a newer version of Xcode, so I installed Xcode the newest version 8.2.1, and kept the old version in a different directory just in case. I don't think the problem is with the installation of Xcode though. Also when I installed CUDA I had to set DYLD_LIBRARY_PATH to a directory. However, it might not be the source of the problem! I am just trying to help you figure out the problem to help me fix it :)
Thank you in advance!
g++ Fig11_05.cpp -o Fig11_05
I spent hours trying to solve this problem by looking up the Internet and particularly stackoverflow, but with no avail!
I tried using different command line arguments such as -libstd=libc++, -std=c++11, -std=c++14
the error I keep getting is as this:
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 ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
The result of g++ -v:
Configured with: --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 version 8.0.0 (clang-800.0.42.1) Target: x86_64-apple-darwin15.6.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
The codes:
// 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
Note: I recently installed then uninstalled CUDA toolkit 8. I needed a newer version of Xcode, so I installed Xcode the newest version 8.2.1, and kept the old version in a different directory just in case. I don't think the problem is with the installation of Xcode though. Also when I installed CUDA I had to set DYLD_LIBRARY_PATH to a directory. However, it might not be the source of the problem! I am just trying to help you figure out the problem to help me fix it :)
Thank you in advance!