Compiling a simple program under UNIX (C++)

Status
Not open for further replies.

caffeine43

Registered Member
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]$
 
caffeine43 ...

Could it be:

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

should be:

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

or

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

Take care :confused:

Oh, and welcome to Sciforums ;)
 
Last edited:
No, I don't think that is it... I've always used cout...what do the other things do? Thanks for the reply :)

-Tom
 
Try this:

#include

float getFloat();

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


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

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?
 
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.
 
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.

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.
 
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:
Have you ever watched a professional programmer at work?

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.
 
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'.
 
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. :(

Take care.
 
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.

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.
 
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.
 
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
 
Status
Not open for further replies.
Back
Top