Thread: function redeclaration gives an error

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    101

    function redeclaration gives an error

    When I build the following I get an error as follows:

    'float computedecimal' redeclared as different kind of symbol
    Previous declaration of 'float computedecimal (int, int, int)'

    Code:
    #include <iostream>
    
    using namespace std;
    
    int degrees {};
    int minutes {};
    int seconds {};
    float decimal{};
    
    const int secondsinminute {60};
    const int  minutesindegree {60};
    
    float computedecimal (int, int, int);
    
    
    int main()
    {
        cout<<"Please insert degrees : ";
        cin>> degrees;
    
        cout<<endl<<endl<<"Please insert minutes : ";
        cin>>minutes;
    
        cout<<endl<<endl<<"Please insert seconds : ";
        cin>>seconds;
    
        decimal=computedecimal (degrees, minutes, seconds);
    
        cout<<endl<<endl<<degrees<<" degrees, "<<minutes<<" minutes, "<<seconds<<" seconds = "
        <<decimal<<"  degrees";
    
        cin.get ();
        cin.get ();
    
        return 0;
    }
    
    
    float computedecimal (degrees, minutes, seconds)
    {
        int top=(minutes*secondsinminutes)+seconds;
        int bottom=secondsinminutes*minutesindegree;
    
        decimal=degrees+(top/bottom);
    
        return decimal;
    }
    I don't understand this as I am passing 3 ints to the function and expecting a float in return.

    Obviously I am doing something wrong but can't figure it out myself.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your function definition:
    Code:
    float computedecimal (degrees, minutes, seconds)
    does not match your function prototype:
    Code:
    float computedecimal (int, int, int);
    You should keep the parameter names in the prototype to avoid this kind of mistake.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    101
    Thanks laserlight.

    I changed the prototype to
    Code:
    float computedecimal (degrees, minutes, seconds);
    But now get 2 errors :
    error : expression list treated as compund expression in initializer [f-permissive]
    and
    'computedecimal' cannot be used as a function

    Did I understand you correctly when I changed the initial declaration?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    No you didn't understand laserlight's meaning.

    Leave the function declaration (aka prototype) "as is" and change the implementation to
    Code:
        float computedecimal (int degrees, int minutes, int seconds);
    You need to be clear in your mind whether the function receives inputs as arguments or as variables declared at file scope (when variables or arguments in different scopes have the same name, you need to be careful about which one is affected by any statement).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    101
    Thanks grumpy,
    That now works as expected, except for one thing - decimal which I declared as a float only produces an integer. Do the variables it uses to compute it also have to be floats, currently they are integers because that is what they will always be but of course when you divide two integers you can end up with a floating point answer.
    I seem to be having problems even with the first chapters of my book, maybne it is because I am a competent BASIC programmer but have never tried C++ before - any tips for getting over that?

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You're the one who specified that computedecimal() accepts three ints, and specified that the variables top and bottom in that function are of type int.

    You may think that dividing two integers yields a floating point answer, but computer hardware specifications and the C standard both disagree with you. Dividing two integers does an integer division, not a floating point division. So 2/3 will be computed as zero (rounded down), and a floating point value will never be computed in that process.

    If you want the division to be done as floating point, then you need to force the issue. One approach is to define top and bottom to be of type float (or double) rather than int: a division that involves a floating point operand is done in floating point. The other way is to convert one of the values to be of type float. If you read your text a bit more closely, you will find that described in early chapters.

    As to being a "competent BASIC programmer" learning C++, the rules of the two languages are quite different, so you can't expect your experience with BASIC will help you all that much with C++. You are better off assuming you know nothing about computer programming, and reading your C++ text without assuming you can skip sections because you know BASIC. Otherwise you will spend more time "unlearning" BASIC than you will learning anything useful about C++. (The same advice would apply, in reverse, to a C++ programmer learning BASIC - with the caveat that BASIC is a much simpler language than C++, so it is easier to learn from scratch).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    101
    Thanks grumpy, good advice which I shall heed.

    I had assumed that if the result was declared as a float then the division would give that result, but now I see the error of my ways, I have actually learnt quite a lot today so I thank you again, I appreciate it must be frustrating having to explain these little intricacies to a beginner, especially one who knows a different language.

  8. #8
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Quote Originally Posted by grumpy View Post
    As to being a "competent BASIC programmer"
    I like how you put that in quotes, very droll.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Actually, what I meant is that your prototype should be:
    Code:
    float computedecimal(int degrees, int minutes, int seconds);
    Then the definition starts with:
    Code:
    float computedecimal(int degrees, int minutes, int seconds)
    {
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error "in function 'main' syntax error before 'int' Help Please
    By blackhat11907 in forum C Programming
    Replies: 5
    Last Post: 08-20-2011, 07:05 PM
  2. enum redeclaration
    By mynickmynick in forum C Programming
    Replies: 4
    Last Post: 02-26-2009, 12:06 PM
  3. redeclaration of enumerator
    By dlittle in forum C Programming
    Replies: 4
    Last Post: 01-13-2009, 11:35 AM
  4. string redeclaration in function, why is required?
    By elninio in forum C++ Programming
    Replies: 6
    Last Post: 07-14-2008, 06:58 PM
  5. Redeclaration Error
    By shiju in forum C Programming
    Replies: 6
    Last Post: 09-02-2004, 07:51 AM