Compiling a simple program under UNIX (C++)

Discussion in 'Computer Science & Culture' started by caffeine43, Feb 25, 2002.

Thread Status:
Not open for further replies.
  1. caffeine43 Registered Member

    Messages:
    7
    Why am I getting an implicit declaration error? I don't know why it is doing this...thanks for help in advance....
    -Tom



    #include <iostream>

    int main()
    {
    float f = 235.325634;
    float g = getFloat();
    cout << f << "\n" << g;
    return 0;
    }


    float getFloat()
    {
    float q = 214.34623;
    return static_cast<float>(215.35623);
    }



    [ Wrote 17 lines ]

    [tlutz@guinness Parser]$ g++ test.cpp -o test
    test.cpp: In function `int main()':
    test.cpp:7: implicit declaration of function `int getFloat(...)'
    [tlutz@guinness Parser]$
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. Chagur .Seeker. Registered Senior Member

    Messages:
    2,235
    caffeine43 ...

    Could it be:

    cout << f << "\n" << g;

    should be:

    count << f << "\n" << g;

    or

    c_out << f << "\n" << g;

    Take care

    Please Register or Log in to view the hidden image!



    Oh, and welcome to Sciforums

    Please Register or Log in to view the hidden image!

     
    Last edited: Feb 26, 2002
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. caffeine43 Registered Member

    Messages:
    7
    No, I don't think that is it... I've always used cout...what do the other things do? Thanks for the reply

    Please Register or Log in to view the hidden image!



    -Tom
     
  6. Google AdSense Guest Advertisement



    to hide all adverts.
  7. Porfiry Nomad Registered Senior Member

    Messages:
    4,127
    Try this:

    You need to declare your functions before you use them, even if they're in the same file. These are, unfortunately, syntactical problems of 20 years ago that no one has bothered to fix (even though the fix would be trivial).


    Also, what's with the '#include'? You're not actually #including anything?
     
  8. caffeine43 Registered Member

    Messages:
    7
    Thanks so much for the help! I hate little problems like that...

    -Tom
     
  9. Bowser Namaste Valued Senior Member

    Messages:
    8,828
    It is the lettle things that trip you up. Wait till your code starts to grow large. You can create the simplest bug, but it will take days to find.
     
  10. Porfiry Nomad Registered Senior Member

    Messages:
    4,127
    I've become quite obsessive about compiling after every 5 new lines of code or so, just to make sure if I've broken something I know exactly where it is.
     
  11. Bowser Namaste Valued Senior Member

    Messages:
    8,828
    LOL! I am the same way; however, sometimes the block you are writing becomes lengthy in itself. I run into problems when I cut and paste. I've caused myself a lot of trouble moving code around. Blocking out code that doesn't work and writing new code under it is another bad habit, my files become a garbage bin of old useless code. I'm still learning, however.

    Have you ever watched a professional programmer at work?
     
    Last edited: Feb 27, 2002
  12. Porfiry Nomad Registered Senior Member

    Messages:
    4,127
    No, but I'm sure it's not a pretty sight -- what with all the sweating, cursing and mashing of keys, not to mention the jiggling rolls of fat and the steam rising off the balding head as he works himself up into a frenzy.
     
  13. Bowser Namaste Valued Senior Member

    Messages:
    8,828
    Good one... Actually, they pound on those keys without stopping for thought, as if the code was flowing off their heads. It's frickin amazin'.
     
  14. Chagur .Seeker. Registered Senior Member

    Messages:
    2,235
    caffeine43 ...

    Sorry about that. And what makes me feel even worse is that the answer
    to the problem was indicated by:

    "test.cpp:7: implicit declaration of function `int getFloat(...)''


    Oh well.

    Please Register or Log in to view the hidden image!



    Take care.
     
  15. Henrik Registered Member

    Messages:
    15
    Hey! We're not all bloaters you know - oh, and I've not got a bald head either, just a receeding hair line! Yeah, ok - I sometimes use colourful language and hit my keyboard, but who doesn't???



    H.
     
  16. Porfiry Nomad Registered Senior Member

    Messages:
    4,127
    I think my apartment-mates occasionally think I've finally gone stark raving mad when they hear incoherent screaming interspersed with agonized groaning coming through the walls. Especially considering I'm normally a quiet and unusually civilized individual.
     
  17. Bowser Namaste Valued Senior Member

    Messages:
    8,828
    Hmm, I just grind my teeth and start pulling books.
     
  18. NAGESH Registered Member

    Messages:
    1
    Using Static_cast Wrongly

    Hi this is the right way of using the static _cast and also u need to Put the Function definiotn before the call. Otherwise u need to declare the function inside the Main.

    The working program looks Like this.

    #include <iostream.h>

    float getFloat()
    {
    float q = 214.34623;
    return static_cast <float>(215.35623);
    }

    int main()
    {
    float f = 235.325634;
    float g = getFloat();
    cout << f << "\n" << g;
    return 0;
    }


    Implicit Declaration is becuase, The compiler assumes getFloat function as returning INT becuase it didnt find one at the time of Binding (meaning Compiling the Main function). So if u declare this as a Forward declaration then also it works Fine.. Even this Piece of code will also work fine without any errors. as Follows


    int main()
    {
    float f = 235.325634;
    float getFloat();
    float g = getFloat();

    cout << f << "\n" << g;
    return 0;
    }


    float getFloat()
    {
    float q = 214.34623;
    return static_cast <float>(215.35623);
    }

    I hope this will help you.
    Nagesh J
    nageshj@indiatimes.com
     
Thread Status:
Not open for further replies.

Share This Page