quick question

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?
it doesn't read "cout" or other simple commands

I don't know what you mean by that.

When I put functions on the .h file

You should not be putting executable code in a .h file. A .h file should only contain declarations.

Beyond those comments, I would have to see the code to make further comments.




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.

If you see the .h below, you'll see the cout etc.

.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include "prime_numbers_automated.h"

using namespace std;


int main()
{


	long long (x);
        cout<<"Input Max. Number for Prime Number distribution: ";
	cin>>x;
	cout<<endl;
	cout<<"You chose "<<x<<endl;
	get_quotient_matrix(x);
	output_quotient_matrix(x);
	return 0;
}


[code/]

[code] .h

#ifndef PRIME_NUMBERS_AUTOMATED_H_
#define PRIME_NUMBERS_AUTOMATED_H_



int get_quotient_matrix(int x)
{
	long long a[x];
	long long result;
	for(int i = 1 ; i<x+1; ++i)
	{
		result = x % i;
		a[i]=result;
	}
}

int output_quotient_matrix(int x)
{
	long long a[x], result;
	for(int i = 1 ; i<x+1; ++i)
		{
			result = x % i;
			a[i]=result;
			cout<<result;
		}
}





#endif /* PRIME_NUMBERS_AUTOMATED_H_ */
Last edited on
First of all: function definitions should not be in header files (unless they are inline).

Second: cin and cout reside in namespace std. Their correct names are std::cin and std::cout

Fixed code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//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
}

void output_quotient_matrix(int x)
{
	long long 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()
{
	long long x;
        std::cout << "Input Max. Number for Prime Number distribution: ";
	std::cin >> x;
	std::cout << "\nYou chose " << x << '\n';
	output_quotient_matrix(x);
}
Last edited on
Thanks for the fix, I'll learn by it.

Did you add the .h code to the .cpp? I can't tell. Seems all of the quote is one the .cpp now.?
I left declarations in .h file
I moved definitions to the .cpp file

And slightly changed main.cpp
Not a newbie but have a newbie question. Why have the definitions in the .h file at all? What does that benefit? I have (2) .cpp files now.

Thanks in advance.

And I would assume that the definitions are also there for implementation.
Last edited on
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)
Last edited on
.cpp
1
2
3
4
5
6
7
8
void output_quotient_matrix(int x)
{
	long long 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.
Last edited on
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.
can u explain why you don't seem to use namespace and what I should do regarding it?

http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice

It is fine to use it in limited scope as long as you know what it exactly does and implications of using it.
ok, I'll read. Thank you for your instruction.
Topic archived. No new replies allowed.