Connecting Tech Pros Worldwide Forums | Help | Site Map

Implicit Declaration of Function Error

Newbie
 
Join Date: Sep 2007
Posts: 3
#1: Sep 24 '07
Hi, I'm very new to coding, so this is a newbie question. I'm trying to print out a banner using functions instead of main:
Expand|Select|Wrap|Line Numbers
  1. int main () {
  2.     banner1 ();
  3. }
  4.  
  5.    void banner1 (void) {
  6.    cout << " Your Banner Here" << endl;
  7. }
When I try to execute the code, I get this error message:

In function `int main()':
7: implicit declaration of function `int banner1(...)'


Placing a preceding declaration such as "int" or "float" in front of "banner1 ()" allows the code to compile, but it does not print anything. How do I get the code to compile and print as a function?
sicarie's Avatar
Moderator
 
Join Date: Nov 2006
Location: USA
Posts: 3,929
#2: Sep 24 '07

re: Implicit Declaration of Function Error


As far as I can tell, the function is called and defined properly there (though i don't think you need the 'void' in the parameter list). Do you have a prototype defined under your includes (above your main)? You also declare int main but don't return anything. Try adding 'return 0; below your banner1() call.
Newbie
 
Join Date: Sep 2007
Posts: 3
#3: Sep 24 '07

re: Implicit Declaration of Function Error


Quote:

Originally Posted by sicarie

As far as I can tell, the function is called and defined properly there (though i don't think you need the 'void' in the parameter list). Do you have a prototype defined under your includes (above your main)? You also declare int main but don't return anything. Try adding 'return 0; below your banner1() call.

I include iostream, iomanip, and cmath (the entire program is pretty long, but the banner is the holdup for now).

I added "return 0;", but with the same results as before.
sicarie's Avatar
Moderator
 
Join Date: Nov 2006
Location: USA
Posts: 3,929
#4: Sep 25 '07

re: Implicit Declaration of Function Error


Did you add the prototype and remove void from the parameter list of the implementation?
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,366
#5: Sep 25 '07

re: Implicit Declaration of Function Error


You need to define a manipulator like endl.

endl is really a function that takes an ostream& argument. The << operator for ostream is set up to call functions by address provided the function takes only one argument that is an ostream&:

Expand|Select|Wrap|Line Numbers
  1. ostream& banner(ostream& os)
  2. {
  3.      os << "xxxxx";
  4.      return os;
  5. }
  6.  
Then you:
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     cout << banner << endl << "Hello" << endl <<banner << endl;
  4. }
  5.  
You will need to #include <iomanip>.
sicarie's Avatar
Moderator
 
Join Date: Nov 2006
Location: USA
Posts: 3,929
#6: Sep 25 '07

re: Implicit Declaration of Function Error


Or you can cheat by putting "Your Banner Here\n" - and sticking the newline character into the string.
Member
 
Join Date: Sep 2007
Posts: 90
#7: Sep 25 '07

re: Implicit Declaration of Function Error


I think you only need to add this before main

void banner1 (void);
sicarie's Avatar
Moderator
 
Join Date: Nov 2006
Location: USA
Posts: 3,929
#8: Sep 25 '07

re: Implicit Declaration of Function Error


Quote:

Originally Posted by Cucumber

I think you only need to add this before main

void banner1 (void);

Yes, that was the prototype I was suggesting to him, though I wanted the OP to figure that out (you know, that whole 'learning process' thing).
Member
 
Join Date: Sep 2007
Posts: 90
#9: Sep 25 '07

re: Implicit Declaration of Function Error


Oh, I failed to see that!

I agree, the best way to learn is by taking the effort to solve a problem rather than getting the answer. Sorry!
sicarie's Avatar
Moderator
 
Join Date: Nov 2006
Location: USA
Posts: 3,929
#10: Sep 25 '07

re: Implicit Declaration of Function Error


Quote:

Originally Posted by Cucumber

Oh, I failed to see that!

I agree, the best way to learn is by taking the effort to solve a problem rather than getting the answer. Sorry!

Eh, not a big deal.
Newbie
 
Join Date: Sep 2007
Posts: 3
#11: Sep 26 '07

re: Implicit Declaration of Function Error


Thanks you all for your help. The code is compiling now, but it's still not printing anything. I think I need to change the return statement (currently return0;), but I'm not entirely sure. I tried replacing it with banner1 and banner1 ();, but they just create error messages. Here's the code as it stands:

Expand|Select|Wrap|Line Numbers
  1.  using namespace std;
  2.  
  3. void banner1 (void);
  4.  
  5. int main () {
  6.    void banner1 ();
  7.    return 0;
  8. }
  9.  
  10.    void banner1 () {
  11.    cout << "YOUR BANNER HERE" << endl;
  12.  
  13. }
Any ideas?

I apologize if these are just simple semantics issues, but again I am very new to programming.
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,366
#12: Sep 26 '07

re: Implicit Declaration of Function Error


Why has Post #5 been ignored ??
Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,428
#13: Sep 26 '07

re: Implicit Declaration of Function Error


Quote:

Originally Posted by weaknessforcats

Why has Post #5 been ignored ??

Using a function that manipulates an ostream is too advanced for the OP to understand?

Anyway, the code you have right now is declaring a new function inside main(). You should not have the return type there - you don't even need the void keyword between the parentheses. Just use banner1();
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,366
#14: Sep 27 '07

re: Implicit Declaration of Function Error


Quote:

Originally Posted by Ganon11

Using a function that manipulates an ostream is too advanced for the OP to understand?

Beginners use endl on the first day. I assume the instructors tell them how it works:
Expand|Select|Wrap|Line Numbers
  1. ostream& endl(ostream& arg)
  2. {
  3.    arg << '\n';
  4.    arg.flush();
  5.    return arg;
  6. }
  7.  
Member
 
Join Date: Sep 2007
Location: New Delhi
Posts: 33
#15: Sep 28 '07

re: Implicit Declaration of Function Error


Hi ,
Find below the running version of ur code:
::No spoonfeeding::
The output is:
YOUR BANNER HERE



Quote:

Originally Posted by weaknessforcats

Beginners use endl on the first day. I assume the instructors tell them how it works:

Expand|Select|Wrap|Line Numbers
  1. ostream& endl(ostream& arg)
  2. {
  3.    arg << '\n';
  4.    arg.flush();
  5.    return arg;
  6. }
  7.  

Reply