Multiple source files

In Microsoft Visual Express you can have more than one source file in a project, but how can i make it so that I have functions spread between them.
IE:

main.cpp
global declarations
int main()


other.cpp
void other()
void another()
Use a header file to store the function prototypes.
sorry, but how would I do that?

at the moment I have something like:

header.h
void other()
void another()

main.cpp
#include "header.h"
int main()


other.cpp
#include "header.h"
void other()
void another()


but it says that the variables that are defined in main.cpp are "undeclared identifiers", would I have to define them in header.h somehow?
Header.h
1
2
3
4
5
6
7
8
#ifndef HEADER_H_ // Include Guards
#define HEADER_H_ 

// Function prototypes
void other();
void another();

#endif 


main.cpp
1
2
3
4
5
#include "Header.h"
int main() {
 other();
 return 0;
}


Header.cpp
1
2
3
4
5
6
7
8
9
#include "Header.h"

void other() {
 // do something
}

void another {
 // do another thing
}

now I have that but when I do:

Header.h
1
2
3
4
5
6
7
#ifndef HEADER_H_ 
#define HEADER_H_ 

void other();
void another();

#endif  



main.cpp
1
2
3
4
5
6
#include "Header.h"
int i(5);
int main() {
 other();
 return 0;
}



header.cpp
1
2
3
4
5
6
7
8
9
#include "Header.h"

void other() {
 i ++;
}

void another {
 coutt << 55 * i
}


the i is an "undeclared identifier"
You would need to declare i outside the function in the Header.cpp - not outside main.cpp

or

main.cpp
1
2
3
4
5
6
#include "Header.h"
int i = 5;
int main() {
 other(i);
 return 0;
}


header.cpp
1
2
3
4
5
6
7
8
9
#include "Header.h"

void other(int temp) {
 temp ++;
}

void another(int temp) {
 cout << 55 * temp
}


EDIT

header.h
1
2
3
4
5
6
7
#ifndef HEADER_H_ 
#define HEADER_H_ 

void other(int temp);
void another(int temp);

#endif  


Sorry forgot to add the header.h
Last edited on
Topic archived. No new replies allowed.