problem with g++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • putnam120
    New Member
    • Nov 2007
    • 1

    #1

    problem with g++

    I am using Visual C++ and when I compile the following code there are no errors or warnings. However, when I compile with g++ I get 3 errors and 1 warning.

    Here is the part of the code that generated the errors.
    ----------------------------------------------------------------------------------------------------------------
    [CODE=cpp]int divsum(int x, vector<int> y) //returns the sum of the PROPER divisors
    {
    int t;
    double sum=1;
    int L=y.size();
    for(t=0; t<L; t++) //go through each element of the list of generated primes
    {
    int e=1;
    double psum=1;
    if(x%y[t]==0)//test for divisibility
    {
    while(fmod(x,po w(y[t],e))==0) //sum of p^e where p^e divides y
    {
    psum=(int) psum+pow(y[t],e);
    e++;
    }
    sum=sum*psum; //formula for divisor sum
    }
    }
    int rsum; rsum= (int)sum;
    return rsum-x;
    }[/CODE]
    -------------------------------------------------------------------------------------------------------------
    The error messages all read something similar to: "divisorsum.cpp :73: error: call of overloaded âpow(int&, int&)â is ambiguous"

    What is the mistake, and how can I avoid making it in the future?
    Last edited by Ganon11; Nov 21 '07, 06:39 PM. Reason: Please use the [CODE] tags provided.
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    pow() is an old C function, which for some strange reason was never written to take two integer parameters or return an integer. You could write your own version, but the valid signatures for <cmath> pow() are:

    double pow(double base, double exponent)
    long double pow(long double base, long double exponent)
    float pow(float base, float exponent)

    You can use a cast to change your int into a double - not a static_cast<dou ble> or anything similar (those are for real emergencies) - just a simple cast and then you could use floor() to return it to integer form. Since your doubles will be whole numbers in this problem, there is no issue.

    [CODE=cpp]
    int a = 4, b = 2;
    int c = floor(pow(doubl e(4), double(2))); //c = 16
    [/CODE]

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      Ah, so our mystery pow() problem seems to be g++ specific? Or, at least, Visual C++ has provided an int pow(int, int) function in their <cmath> header file.

      Anyway, your warning is probably, "WARNING: no newline at end of file." I think this is a ridiculously silly warning, but you can get rid of it by entering your source code file, pressing Ctrl+End to go to the end of your file, pressing Enter/Return once, and saving.

      Comment

      Working...