Header and .cpp Files issues :/ HELP

closed account (ivDwAqkS)
I don't know whats going on, I am new on using header and cpp files I always did it in one file, it works in 1 .cpp file, however when I write it in diferent files it doesn't work, can someone help me and tell whats going on? it doesn't pop up error, but its not the cout what its suppose to print. can someone help me? D:

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//header Data.h
#include <iostream>
using namespace std;

#ifndef DATA_H
#define DATA_H
extern int arr[10];
void function();
void function2();
void my_swap(int *, int *);
void sorted_numbers();
#endif
//main.cpp
#include "Data.h"

int arr[10];

int main(){

	function();
	cout << endl << endl;
	cout << arr[1]<<"\n\n";
	function2();
	sorted_numbers();
	function2();
	return 0;
}
//function.cpp
#include "Data.h"

void function(){
	for (int i = 0; i < 10; i++){
		cout << "Enter arr[" << i << "]: ";
		cin >> arr[i];
	}
}
//function2.cpp
#include "Data.h"

void function2(){
	for (int i = 0; i < 10; i++){
		cout << arr[i] << " ";
	}
}
//my_swap.cpp
#include "Data.h"

void my_swap(int *a, int *b){
	int temp;
	temp = *a;
	*a = *b;
	*b = *a;
}
//swap_lower.cpp
#include "Data.h"

void sorted_numbers(){
	int i, j, low;
	cout << "\n\n";
	for (i = 0; i < 10; i++){
		low = i;
		for (j = i + 1; j < 10; j++){
			if (arr[j] < arr[low]){
				low = j;
			}
		}
		if (i != low){
			my_swap(&arr[i], &arr[low]);
		}
	}		
}

I just started to use Visual Studio, and its weird coz when I take out some braces when the statement is only one line, it prints me different answer for example
1
2
3
for (j = i + 1; j < 10; j++)
	if (arr[j] < arr[low])
		low = j;

if I write it like that, it prints me different, why? >.>. I used Code::blocks before, though.
Last edited on
Well first you don't need a cpp file for each function just put them all in Data.cpp if the file was big with a lot of functions then yea multiple files are needed. Secondly, extern int arr [10] is a global variable in fact I would switch the extern for a const.

If you don't know what I mean by a global variable see:
http://en.wikipedia.org/wiki/Global_variable

Accordingly, this means we don't actually need to declare int arr twice as every function access that array. However, to be hones I would suggest getting rid of the array in the header and pass the array in the main.cpp as a pointer. For that see,

http://stackoverflow.com/questions/11829830/c-passing-an-array-pointer-as-a-function-argument

Last edited on
closed account (ivDwAqkS)
but when I try to make my array global it doesn't work and when I put const it also doesn't work coz it doesn't let me change my values or input my array values coz of the const, but when I declared my array in main and putted as extern in the header, worked fine and now I don't know what I am doing wrong, and I am making different files to test and learn a little bit more about headers and cpp files assembly, I am totally confuse about it, coz I can do it all in one file, but my professor is trying to make us using different files, and I also think its kind of professional.

can you explain me a little bit how to declare global arrays without values in the header? I want to global declare it and then input the values.
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
/*Data.h
I declared named arr to my array;
*/
#include <iostream>
using namespace std;

#ifndef DATE_H
#define DATE_H
int arr[10] = {};
void function();
#endif
/*main.cpp
**********************/
#include "Data.h"

int main(){

	function();
	cout << endl << endl;
	return 0;
}
/*function.cpp
it says that here is the error, that my arr is already defined
***********************/
#include "Data.h"
void function(){
	for (int i = 0; i < 10; i++){
		arr[i] = i + 1;
		cout << arr[i] << " ";
	}
}
/*Error	1	error LNK2005: "int * arr" (?arr@@3PAHA) already defined in function.obj
Error	2	error LNK1169: one or more multiply defined symbols found*/

This is what happen when I try to do so.
data.h
1
2
3
4
5
6
7
8
9
10
11
#ifndef DATA_H
#define DATA_H

extern int arr[10];

void function();
void function2();
void my_swap(int *, int *);
void sorted_numbers();

#endif 


data.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
#include <iostream>
#include "data.h"

using std::cout;
using std::cin;

int arr[10];

void function() {
    for (int i = 0; i < 10; i++){
        cout << "Enter arr[" << i << "]: ";
        cin >> arr[i];
    }
}

void function2(){
    for (int i = 0; i < 10; i++){
        cout << arr[i] << " ";
    }
}

void my_swap( ... ) {

}

void sorted_numbers( ... ) {

}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include "Data.h"

int main() {
    using namespace std; // or better, use using directive

    function();
    cout << endl << endl;
    cout << arr[1]<<"\n\n";
    function2();
    sorted_numbers();
    function2();
    return 0;
}


I recommend just declaring the array in main, then pass it into the functions
( u need to change the functions to accept a pointer )
Last edited on
Topic archived. No new replies allowed.