我是一名新的C ++程序员,我有一个C ++文件,我试图在安装了macOS Sierra的MacBook Pro上编译。当我第一次尝试用以下代码编译它时:
g++ -std=c++11 Main.cpp -o rpns
我收到了这个错误:
Undefined symbols for architecture x86_64:
"eval_expr(std::__1::stack<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::deque<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >&, std::__1::stack<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::deque<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >&, std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, int, std::__1::less<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, int> > >)", referenced from:
_main in Main-2c97f0.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
然后我找到了一些答案,说我应该这样编译:
g++ -std=c++11 -stdlib=libstdc++ Main.cpp -o rpns
然后我收到了这个错误:
Main.cpp:105:11: error: use of undeclared identifier 'stoi'
ind = stoi(n, &sz);
^
Main.cpp:120:11: error: use of undeclared identifier 'stoi'
ind = stoi(n, &sz);
^
Main.cpp:133:10: error: use of undeclared identifier 'to_string'
return to_string(len);
^
Main.cpp:138:10: error: use of undeclared identifier 'to_string'
return to_string(ind);
^
Main.cpp:146:9: error: use of undeclared identifier 'stoi'
x = stoi(num1, &sz);
^
Main.cpp:147:9: error: use of undeclared identifier 'stoi'
y = stoi(num2, &sz);
^
Main.cpp:155:11: error: use of undeclared identifier 'to_string'
return to_string(x + y);
^
Main.cpp:163:9: error: use of undeclared identifier 'stoi'
x = stoi(num1, &sz);
^
Main.cpp:164:9: error: use of undeclared identifier 'stoi'
y = stoi(num2, &sz);
^
Main.cpp:172:11: error: use of undeclared identifier 'to_string'
return to_string(x - y);
^
10 errors generated.
这是Main.cpp的代码:
#include <iostream>
#include <string>
#include <stack>
#include <vector>
#include <map>
#include <stdexcept>
using namespace std;
void init_map(map<string, int>& operatorToNumOperands);
string eval_expr(stack<string> &operands, stack<string> &operators, map<string, int> optor_to_num_opnd);
string operator_tilde(const string s1, const string s2);
string operator_right(const string s, const string n);
string operator_left(const string s, const string n);
string operator_len(const string& s);
string operator_find(const string& s, const string& f);
string operator_plus(const string num1, const string num2);
string operator_minus(const string num1, const string num2);
int main() {
return 0;
}
void init_map(map<string, int>& operatorToNumOperands) {
}
string eval_expr(stack<string> &operands, stack<string> &operators, const map<string, int> &optor_to_num_opnd) {
while(!operators.empty()) {
int numOperands = optor_to_num_opnd.at(operators.top());
vector<string> opands;
while(numOperands > 0) {
if(operands.empty())
throw "not enough arguments given for: '" + operators.top() + "' operator";
opands.push_back(string(operators.top()));
numOperands--;
}
string expr_val = "";
try {
if(operators.top() == "~") {
expr_val = operator_tilde(opands[1], opands[0]);
} else if(operators.top() == "->") {
expr_val = operator_right(opands[1], opands[0]);
} else if(operators.top() == "<-") {
expr_val = operator_left(opands[1], opands[0]);
} else if(operators.top() == "#") {
expr_val = operator_len(opands[0]);
} else if(operators.top() == "?") {
expr_val = operator_find(opands[1], opands[0]);
} else if(operators.top() == "+") {
expr_val = operator_plus(opands[1], opands[0]);
} else {
expr_val = operator_minus(opands[1], opands[0]);
}
operators.pop();
} catch (const invalid_argument& ia) {
throw invalid_argument("InvalidArgumentError: " + string(ia.what()) + "\n");
} catch (const out_of_range& oor) {
throw out_of_range("OutofRangeError" + string(oor.what()) + "\n");
}
operands.push(expr_val);
}
return string(operands.top());
}
我几个小时都被困在这两个错误上。提前感谢您的帮助。