c++ - ld : symbol(s) not found for architecture x86_64 on OSX 10. 9

标签 c++ macos xcode6 stack

我正在我的 Xcode 6.2 上执行 C++ 程序,但出现此错误:

Undefined symbols for architecture x86_64:
  "operator+(Stack1<int> const&, Stack1<int> const&)", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我不确定如何继续解决这个问题,我在 Stackoverflow.com 上检查了几个其他人但无法解决我的问题,

#include <iostream>
#include <vector>

using namespace std;

template <class T>
class Stack1;

template <class T>
Stack1<T> & operator+(const Stack1<T> &x, const Stack1<T> &y) {
Stack1<T> z = x;
for (unsigned int i=x.size(); i<(x.size()+y.size()); i++) {
    z.push(y[i]);
}
return z;
}

template <class T>
class Stack1 {
friend Stack1<T> & operator+(const Stack1<T> &x, const Stack1<T> &y);
private: vector <T> elems;
public: bool empty();
void push(const T &item);
T & top();
void pop();
long size();
};

template <class T>
bool Stack1<T>::empty() {
return elems.empty();
}

template <class T>
void Stack1<T>::push(const T &item){
elems.push_back(item);
}

template <class T>
T &Stack1<T>::top(){
   return elems.back();
}

template <class T>
void Stack1<T>::pop() {
if (!elems.empty())
    return elems.pop_back();
}

template <class T>
long Stack1<T>::size() {
return elems.size();
}




int main(int argc, const char * argv[]) {
Stack1 <int> intStack;
Stack1 <float> floatStack;
Stack1 <string> stringStack;
Stack1 <int> a;
Stack1 <int> b;

intStack.push(7);
intStack.push(3);
intStack.push(0);
intStack.push(8);

floatStack.push(0.9);
floatStack.push(4.78);
floatStack.push(2.157);

stringStack.push("test1");
stringStack.push("abc");

while (!intStack.empty()) {
    cout << "Popping from intStack: " << intStack.top() << endl;
    intStack.pop();
}

if (intStack.empty()) {
    cout << "intStack is empty" << endl;
}

while (!floatStack.empty()) {
    cout << "Popping from intStack: " << floatStack.top() << endl;
    floatStack.pop();
}

if (floatStack.empty()) {
    cout << "floatStack is empty" << endl;
}

while (!stringStack.empty()) {
    cout << "Popping from intStack: " << stringStack.top() << endl;
    stringStack.pop();
}

if (stringStack.empty()) {
    cout << "stringStack is empty" << endl;
}

for (int i=0; i<3; i++) {
    a.push(i);
}
for (int i=9; i>5; i--) {
    b.push(i);
}
 //  cout << "Size of a:" << a.size();
Stack1 <int> c;
c = a+b;

while (!c.empty()) {
    cout << "Popping from c: " << c.top() << endl;
    c.pop();
}

return 0;
}

得到这个错误:

  error: failed to launch '/Users/User-name/Library/Developer/Xcode/DerivedData/4_TemplatedStack-bmcfhzdrgyhhybajmxvpyuypgmag/Build/Products/Debug/4_TemplatedStack'

最佳答案

评论中提到的问题是指您代码中的一个危险错误,但与链接器问题无关。您需要做的是添加 <>operator+ 之后:

friend Stack1<T> & operator+ <>(const Stack1<T> &x, const Stack1<T> &y);

然后,long size()需要是 const如果你想在 const 上调用它对象。

无论如何。请阅读this guide on operator overloading . operator+不应返回引用,尤其是局部变量。如果有的话,它的返回值应该是 const T .还有其他问题,但我将其留给您作为练习。

关于c++ - ld : symbol(s) not found for architecture x86_64 on OSX 10. 9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32408685/

相关文章:

c++ - 如何为每个派生类存储一个可以从基类访问的魔数(Magic Number)?

c++ - 更新 std::set 以仅保留最小值

c++ - 将 vector<vector<int>> 指针传递给参数?

使用 Xcode 6 XLIFF 导出的 iOS 本地化/国际化设置 Bungle

swift - 如何从另一个 Swift 文件导入一个 Swift 文件?

c++ - 是否存在 x + 1 == x 类型的 float 值 x?

python - 我正在尝试使用 brew 安装 postgresql,但找不到 Python.h

macos - 在 Mac OSX 10.9.2 上未链接 libpng

PHP 无法加载 Imagick 库 - PHP 启动 : Unable to load dynamic library

ios - xcode 项目中的应用程序没有改变