所以我已经浏览了一段时间来弄清楚这是怎么回事,但很多时候是因为一个程序缺少一个main
或者什么,但我的程序有所有需要的方法
#include <iostream>
#include <string>
#include <sstream>
#include <boost/multiprecision/cpp_int.hpp>
namespace mp = boost::multiprecision;
typedef mp::cpp_int my_int;
bool isPalimdrome(int);
bool isBinaryPalimdrom(std::string);
std::string base10To2(int);
std::string reverse(std::string);
int main() {
int num = 0;
int sum = 0;
while (num < 1000000) {
std::string num_bin = base10To2(num);
if (isPalimdrome(num) && isBinaryPalimdrom(num_bin) ){
sum += num;
}
num++;
}
return 0;
}
bool isPalimdrome(int x) {
std::ostringstream strs;
strs << x;
const std::string ori = strs.str();
const std::string rev = reverse(ori);
if (rev.compare(ori) == 0) return true;
return false;
}
bool isBinaryPalimdrome(std::string s) {
const std::string ori = s;
const std::string rev = reverse(ori);
if (rev.compare(ori) == 0) return true;
return false;
}
std::string base10To2(int x) {
std::string out = "";
while (x != 0) {
if (x % 2 == 0)
out += "0";
else
out += "1";
x /= 2;
}
return out;
}
std::string reverse(std::string s) {
std::string result = "";
for (const char& x : s) {
result = x + result;
}
//std::cout << "This is the original " <<
// s << std::endl;
//std::cout << "This is the reversed " <<
// result << std::endl;
return result;
}
我发布完整代码以防万一,但我认为它与我的isBinaryPalimdrome()
函数有关,这是我得到的错误:
Undefined symbols for architecture x86_64:
"isBinaryPalimdrom(std::string)", referenced from:
_main in Problem36-cae975.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [Problem36] Error 1
如果重要的话,我正在终端的Mac上编程。
答案 0 :(得分:1)
我猜错字导致疼痛: 你宣布:
bool isBinaryPalimdrom(std::string);
但你定义了
bool isBinaryPalimdrome(std::string s)
// ^