c++ - 在C++中递归地从十进制到八进制。以std::string格式输出

标签 c++ c++11 recursion stdstring octal

我正在尝试在C++中将十进制整数十进制化为八进制字符串。我尝试了以下代码,但未成功。辅助函数to_string本身可以很好地工作。问题似乎出在递归函数调用中。

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

string to_string(long num)
{
    ostringstream os;
    os << num;
    return os.str();

}

string dec_2_oct(long dec)
{
    if (dec == 0)
    {
        string s = "";
        return s;
    }
    return dec_2_oct(dec / 8) + to_string(dec % 8);
}

int main() {
    long dec;
    cin >> dec;
    string s;
    s = dec_2_oct(dec);
    cout << s;
}

我看起来很讨厌。
Compiling failed with exitcode 1, compiler output:
prog.cpp: In function 'std::__cxx11::string dec_2_oct(long int)':
prog.cpp:21:50: error: call of overloaded 'to_string(long int)' is ambiguous
     return dec_2_oct(dec / 8) + to_string(dec % 8);
                                                  ^
prog.cpp:6:8: note: candidate: std::__cxx11::string to_string(long int)
 string to_string(long num)
        ^~~~~~~~~
In file included from /usr/include/c++/7/string:52:0,
                 from /usr/include/c++/7/bits/locale_classes.h:40,
                 from /usr/include/c++/7/bits/ios_base.h:41,
                 from /usr/include/c++/7/ios:42,
                 from /usr/include/c++/7/ostream:38,
                 from /usr/include/c++/7/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/7/bits/basic_string.h:6264:3: note: candidate: std::__cxx11::string std::__cxx11::to_string(long double)
   to_string(long double __val)
   ^~~~~~~~~
/usr/include/c++/7/bits/basic_string.h:6255:3: note: candidate: std::__cxx11::string std::__cxx11::to_string(double)
   to_string(double __val)
   ^~~~~~~~~
/usr/include/c++/7/bits/basic_string.h:6246:3: note: candidate: std::__cxx11::string std::__cxx11::to_string(float)
   to_string(float __val)
   ^~~~~~~~~
/usr/include/c++/7/bits/basic_string.h:6240:3: note: candidate: std::__cxx11::string std::__cxx11::to_string(long long unsigned int)
   to_string(unsigned long long __val)
   ^~~~~~~~~
/usr/include/c++/7/bits/basic_string.h:6234:3: note: candidate: std::__cxx11::string std::__cxx11::to_string(long long int)
   to_string(long long __val)
   ^~~~~~~~~
/usr/include/c++/7/bits/basic_string.h:6228:3: note: candidate: std::__cxx11::string std::__cxx11::to_string(long unsigned int)
   to_string(unsigned long __val)
   ^~~~~~~~~
/usr/include/c++/7/bits/basic_string.h:6223:3: note: candidate: std::__cxx11::string std::__cxx11::to_string(long int)
   to_string(long __val)
   ^~~~~~~~~
/usr/include/c++/7/bits/basic_string.h:6217:3: note: candidate: std::__cxx11::string std::__cxx11::to_string(unsigned int)
   to_string(unsigned __val)
   ^~~~~~~~~
/usr/include/c++/7/bits/basic_string.h:6212:3: note: candidate: std::__cxx11::string std::__cxx11::to_string(int)
   to_string(int __val)
   ^~~~~~~~~

任何帮助,将不胜感激!

最佳答案

尝试了解错误:

Compiling failed with exitcode 1, compiler output:
prog.cpp: In function 'std::__cxx11::string dec_2_oct(long int)':
prog.cpp:21:50: error: call of overloaded 'to_string(long int)' is ambiguous
     return dec_2_oct(dec / 8) + to_string(dec % 8);

错误中的关键字是overloadedambiguous,当然还有函数本身。
当您了解这些词在C++上下文中的含义时,您将了解错误的原因。

然后,您可以采用以下方法之一:
  • 将函数的名称从to_string更改为类似my_to_string。这是幼稚的方法。
  • 避免在全局范围甚至全局范围内导入所有std命名空间
    函数范围,并从现在开始遵循std::function的用法。
  • 关于c++ - 在C++中递归地从十进制到八进制。以std::string格式输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51859782/

    相关文章:

    c++ - 如果我需要在我的 header 中包含另一个 header 并且我计划在我的 CPP 文件中使用它,我是否也应该将它包含在那里以提高可读性?

    带有接口(interface)对象的 C++ 模板

    python - 递归算法如何在此代码段中工作?

    c++ - 遍历字符串 vector ,从控制台获取输入,给出段错误

    c++ - 如何将一个无符号的 short 一分为二

    c++ - 为什么 std::array 没有一个构造函数来为要填充的数组取一个值?

    c# - 从字符串生成子字符串的组合

    javascript - 递归异步函数

    c++ - C++11 中的 thread_local 是什么意思?

    c++ - constexpr 函数何时在编译时进行评估?