我執行我的Xcode 6.2 C++程序,但我得到這個錯誤:LD:符號(S)沒有發現建築x86_64的對OSX 10.9
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'
在運營商+'你創建並返回類型的實例'棧1'即使返回類型爲'堆棧&''的定義。這是不允許的 - 你返回一個對象的引用,該對象將在函數執行後被刪除。 –
HelloWorld