Monday, May 24, 2010

Simple C++ source file and function problem?

I'm trying to create a simple program that prints the word "Hello" to the console. However, I want the program to use two different functions, the first being the main and the second being a void function that prints the word to the console. When I try to compile, it tells me there is an error at second2.h on the line that tries to print to the console (the line with cout). I was wondering what I did wrong.


Here is the code from main.cpp:


--------------------------------------...


include %26lt;cstdlib%26gt;


#include %26lt;iostream%26gt;


#include "second2.h"


using namespace std;





void sayHello();


int main(int argc, char *argv[])


{


sayHello();


system("PAUSE");


return EXIT_SUCCESS;


}


--------------------------------------...


the code from second2.h:


--------------------------------------...


#include %26lt;cstdlib%26gt;


#include %26lt;iostream%26gt;


void sayHello()


{


cout %26lt;%26lt; "Hello" %26lt;%26lt; endl;


}


--------------------------------------...





Thanks.

Simple C++ source file and function problem?
Just a guess:





Try moving the using namespace std; before the #include "second2.h", which, as someone else has pointed out, shouldn't include the sayHello() function definition, just its declaration.





I'm thinking your compiler requires the namespace be already pulled in for cout, which, as your code is currently configured, it isn't when sayHello() is compiled.





Good luck.





By the way, any compiler that just says there's an error on line such-and-such without giving any details should be evicted from your machine..
Reply:It cant compile, first line should be #include... and not include...


you should not declare sayHello in the cpp file it is already included in second2.h


I hope EXIT_SUCCESS is defined I don't use this macro.
Reply:You do have a compiler issue (your exact program compiles with Borland for example with the extra # for include first line) but you should be able to compile this with more rigid code.





Function declarations should be in header files so that other .c files can see them without explicitly extern-ing them.





Function definitions should always be in .c files.





That will suit all compilers.





So second2.h should just have





extern void sayHello();





Ths means there is a single place where the function is extern-ed and made available to other files.





Your main file





void sayHello()


{


cout %26lt;%26lt; "Hello" %26lt;%26lt; endl;


}





int main(int argc, char *argv[])


{


sayHello();


system("PAUSE");


return EXIT_SUCCESS;


}





If no other file requires the sayHello function then it ought to be declared as static in the same file as the function definition.
Reply:--- main.cpp ---


#include %26lt;iostream%26gt;


#include "2nd.h"


int main()


{


hello();


system("PAUSE");


}





--- 2nd.h ---


#include %26lt;iostream%26gt;


using namespace std;


void hello()


{


cout %26lt;%26lt; "Hello World!\n";


}


No comments:

Post a Comment