I like to compartmentalize my coding in general. Lately I have been dealing with an app that prints a lot of data to the console, and Ive got code bloat as a result.(the app works though).I am creating functions to clean it up but when I put functions on the .h file it doesn't read "cout" or other simple commands. How do I print from a function in a .h? I have used namespace but that doesn't look right to me.
Thanks in advance
I could post the code, but it seems pretty straight forward?
This is a stripped down incomplete program: the parent works fine. This version will we more organized, etc. I've stopped though because of the .h issue.
it doesn't read "cout" or other simple commands
if I put a cin or cout in the .h if flags an error which is remedied by "namespace std"; in the .h file as well as the .cpp.
//prime_numbers_automated.h
#ifndef PRIME_NUMBERS_AUTOMATED_H_
#define PRIME_NUMBERS_AUTOMATED_H_
int get_quotient_matrix(int x);
void output_quotient_matrix(int x);
#endif /* PRIME_NUMBERS_AUTOMATED_H_ */
//prime_numbers_automated.cpp
#include <iostream>
int get_quotient_matrix(int x)
{
//long long a[x]; //ILLEGAL C++
return 0; //Fixes UB, but unction still not working as it should
}
voidoutput_quotient_matrix(int x)
{
longlong result;
for(int i = 1 ; i<x+1; ++i) {
result = x % i;
std::cout<<result;
}
}
//main.cpp
#include <iostream>
#include "prime_numbers_automated.h"
int main()
{
longlong x;
std::cout << "Input Max. Number for Prime Number distribution: ";
std::cin >> x;
std::cout << "\nYou chose " << x << '\n';
output_quotient_matrix(x);
}
Having declarations in a separate (.h) file is very useful when creating libraries for classes or functions.
The .h file tells the compiler how to call the function or class. The implementation is left to the .cpp file. If you wish, you can make a library header and object file available for others to use without releasing the implementation details.
By putting only declarations in a .h file, you also avoid the problem of multiple defined functions.
For example if two .cpp files include the same header and that header has function definitions, the linker will complain about the existence of the the same function multiple times (i.e. it compiled each time its included).
> First of all: function definitions should not be in header files (unless they are inline).
or template
> Why have the definitions in the .h file at all? What does that benefit?
Compilation may be a lenghty process. If you have definitions in your headers you have to recompile all of them for any minuscule change in your main.cpp (that may be completely unrelated to those functions)
void output_quotient_matrix(int x)
{
longlong result;
for(int i = 1 ; i<x+1; ++i) {
result = x % i;
std::cout<<result;
}
}
.hpp
1 2
int get_quotient_matrix(int x);
void output_quotient_matrix(int x);
It seems that the declaration and the definition/implementation share essentially some of the same information. Why? There is a redundancy.
In both we learn its a void type function, it passes an integer. I am still trying to work out the logic of it. Why need both.
>>The .h file tells the compiler how to call the function or class.
once the compiler calls the function or class in the .h file, how does that call the definition in .cpp?
I think I get it! Let me know: .h files catalog the classes and declarations for users without divulging the implementations (obj file). The redundancy in information is to link one h declaration to a cpp definition/implementation. Also I seeit reducing duplicate functions code etc. Then its really urgent to exploit public, private (very important) and protected class to protect the implementation from being exposed.
It is more to that than to simply hide information about implementation:
If you place implementation in header file, you will be only able to include that header once in your program. If you will include it second time, you will have two copies of function floating around which violates One Definition Rule and leads to error. Remember: #include is just glorified copy-paste.