Compile error, could someone take a look?

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
Ashganek
New poster
Posts: 8
Joined: Thu Jan 19, 2006 10:42 pm

Compile error, could someone take a look?

Post by Ashganek » Tue Jan 24, 2006 5:16 pm

Could someone compile it using gcc and show me what is wrong (since it works in dev-cpp).

Code: Select all

#include <cstdlib>
#include <iostream>

using namespace std;

typedef struct {
        int len;
        char digits[100];
} bint;

bint *results[251];

void init_bint(bint *b)
{
      for( int i = 0; i < 100; i++ )
           b->digits[i] = (char)(0); 
}

bint *int_to_bint(int n)
{
     int i, t;

     bint *b = new bint;
     init_bint(b);
     b->len = -1;
     
     t = n;
     
     while(t > 0) {
             b->len++;
             b->digits[b->len] = (char)(t%10);
             t = t / 10;
     }
     
     if( n == 0 ) b->len = 0;
     return b;
}

bint *add_bint(bint *a, bint *b)
{
     int carry, i;
     
     bint *c = new bint;
     init_bint(c);
     
     c->len = max(a->len, b->len)+1;
     carry = 0;
     
     for( i = 0; i <= (c->len); i++ )
     {
          c->digits[i] = (char)((carry+a->digits[i]+b->digits[i]) % 10);
          carry = (carry+a->digits[i]+b->digits[i])/10;
     }
     
     if( (int)c->digits[c->len] == 0 ) c->len--;
     return c;
}

bint *ways(int n)
{
    if(  n == 1 or n == 0 ) { return int_to_bint(1); }
    else if( results[n] != '\0' ) { return results[n]; }
    
    
    bint *d1 = add_bint(ways(n-1), add_bint(ways(n-2), ways(n-2)));
    results[n] = d1;
    return results[n];
}

void print_bint(bint *b)
{
     for( int v = b->len; v >=0; v-- )
          cout << (int)b->digits[v];
     cout << "\n";
}

int main(int argc, char *argv[])
{
    int n;

    while( cin >> n ) {
           print_bint(ways(n));
    }
}

chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post by chunyi81 » Wed Jan 25, 2006 4:12 am

I compiled your code using g++ 2.95 and the following compiler messages were returned.

testing.cpp: In function `struct bint * add_bint(bint *, bint *)':
testing.cpp:46: implicit declaration of function `int max(...)'
testing.cpp: In function `struct bint * ways(int)':
testing.cpp:61: parse error before `n'
testing.cpp:61: confused by earlier errors, bailing out

There is a max function inside algorithm library, and I presume you are using that function? You need to #include <algorithm> to use it. Dev C++ implicitly includes the necessary headers for you.

Also, how on earth did this line compile in Dev C++ in the first place?

Code: Select all

    if(  n == 1 or n == 0 ) { return int_to_bint(1); } 
Do you mean

Code: Select all

    if (n == 1 || n == 0) { return int_to_bint(1); }
By the way, your code compiles using g++ 3.3.2, which is around the same version as the g++ compiler used by Dev C++.

Post Reply

Return to “C++”