1

I'm trying to decimal integers into Octal Strings recursively in C++. I have tried the following code with no success. The helper function to_string works good by itself. The problem seems to be in the recursive function call.

#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;
}

I'm getting pretty nasty looking errors.

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)
   ^~~~~~~~~

Any help would be appreciated!

Vasu Malhotra
  • 13
  • 1
  • 4
  • 5
    Just in case you didn't know this existed: [`std::to_string`](https://en.cppreference.com/w/cpp/string/basic_string/to_string). [Don't abuse `using namespace std`. (i.e. just don't do it at all).](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – WhozCraig Aug 15 '18 at 13:42
  • 2
    Because you import all in `std` namespace and there is also `std::to_string(long)` so `to_string(dec % 8)` ambiguous here. Change into `::to_string(dec % 8)`, otherwise even better stop using so-yesterday idiom `using namespace std;` and call functions with `std::` prefix instead. – sandthorn Aug 15 '18 at 13:45
  • 2
    And why not just use [`std::cout << std::oct << dec << '\n';`](https://en.cppreference.com/w/cpp/io/manip/hex) ? – WhozCraig Aug 15 '18 at 13:50
  • 2
    If you need the octal into a string: `std::ostringstream s; s << std::oct << dec << variable; std::string result (s.str());` – Thomas Matthews Aug 15 '18 at 13:58
  • @WhozCraig My aim was to learn recursion by application. Though your method is definitely better. – Vasu Malhotra Aug 15 '18 at 17:12
  • And yes I will avoid 'using namespace std;' from now on! Thanks. – Vasu Malhotra Aug 15 '18 at 17:16

1 Answers1

0

Try to understand the error:

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);

The keywords in the error are overloaded, ambiguous and of course the function itself. When you understand the meanings of these words in C++ context, you will understand the reason for the error.

Then you can take one of the following approaches:

  1. Change the name of your function from to_string to something like my_to_string. This is the naive approach.
  2. Avoid importing all the std namespace at global scope or even function scope and follow the usage of std::function from now on.
P.W
  • 26,289
  • 6
  • 39
  • 76