开发者

Trying to make a recursive call in C++

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(in开发者_运维百科t&)’
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.


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


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.


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 ) 


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜