tags:

views:

87

answers:

4

Hello Stack Overflow! This is my first question here so be kind :-) I'm trying to make a recursive call here, but I get the following compiler error:

In file included from hw2.cpp:11:
number.h: In member function ‘std::string Number::get_bin()’:
number.h:60: error: no matching function for call to ‘Number::get_bin(int&)’
number.h:27: note: candidates are: std::string Number::get_bin()

    string get_bin () 
        {
              bin = "";
              printf("Value is %i\n",val);
              if (val > 0)
            {
              int remainder = val;
              printf("remainder is %i\n",remainder);
              printf("numbits is %i\n",size);
              for (int numbits = size-1;numbits>=0;numbits--)
                {
                  //printf("2 raised to the %i is %i\n",numbits,int(pow(2,numbits)));
                  printf("is %i less than or equal to %i\n",int(pow(2,numbits)),remainder);
                  if (int (pow(2,numbits))<=remainder)
                {   


                  bin+="1";
                  remainder -= int(pow(2,numbits));

                  printf("Remainder set to equal %i\n",remainder);
                }
                  else
                {
                  bin+= "0";
                }

                }
              return bin;
            }
              else
            {
              int twoscompliment = val + int(pow(2,size));
              return get_bin(twoscompliment);
            }

Any thoughts? I know get_bin works for positive numbers.

+2  A: 

In the last line you are calling get_bin() with an integer reference argument, but there are no formal parameters in the function signature.

Arnold Spence
Thanks, Wow that was a quick answer.
b.j.g
The C++ crew here is pretty sharp. I don't usually finish typing before 3 answers arrive :) The other answers have some good additional info over mine.
Arnold Spence
+2  A: 
string get_bin ()
return get_bin(twoscompliment);

These are mutually incompatible. I don't see how you can say that code works for positive numbers since it's not even compiling.

You probably need to change the first line to something like:

string get_bin (int x)

but, since you don't actually use the argument, you may have other problems.

If you're using global or object-level variables to do this work, recursion is not going to work, since they different levels will be stepping on each other's feet (unless you do your own stack).

One of the beauties of recursion is that your code can be small and elegant but using local variables is vital to ensure your data is level-specific.

By way of example, examine the following (badly written) pseudo-code:

global product
def factorial (n):
    if n == 1:
        return 1
    product = factorial (n-1)
    return n * product

Now that won't work for factorial (7) since product will be corrupted by lower levels. However, something like:

def factorial (n):
    local product
    if n == 1:
        return 1
    product = factorial (n-1)
    return n * product

will work just fine as each level gets its own copy of product to play with. Of course:

def factorial (n):
    if n == 1:
        return 1
    return n * factorial (n-1)

would be even better.

paxdiablo
I ended up copying the function nested in the if statement and overloading the get_bin method. So it's no longer a recursion because I cant eliminate my global variable as I'm building this to a spec for a class. and we must use a global variable.
b.j.g
A: 

The function is defined to take no arguments, yet you pass an int.

It looks like you're accessing a global or member variable val. That should probably be converted into the argument.

string get_bin ( int val ) 
Potatoswatter
A: 

Since you have not declared bin and val in the function I guess they are global.

Now you define the function get_bin() to return a string and not accept anything. But in the recursive call you are passing it an int. Since you want to pass twoscompliment as val for the recursive call you can do:

int twoscompliment = val + int(pow(2,size));
val = twoscompliment; // assign twoscompliment to val
return get_bin();
codaddict
I cant have val change because its storing the negative number in decimal, which must be retained. Thanks for the help though, I see what you are saying.
b.j.g