I'm now working on a project and trying to organize the working code for later reproduction. And then I encountered the famous redefinition error and unfortunately, none of the answers hit the spot. So I simplified my problem and here it is.
//main.c
#include <iostream>
#include "main.h"
#include "myfunc1.h"
int func2();
int func3();
int func4();
int main(int argc, char** argv) {
func1();
func2();
func3();
func4();
return 0;
}
/* Trying to seperate func1*/
int func2(){
int num2 = rep_val *2;
int i = 0;
for(i=0;i<num2;i++){
printf("HO!\n");
}
}
int func3(int rep){
int num3 = rep_val *3;
int i = 0;
for(i=0;i<num3;i++){
printf("HEY!\n");
}
}
int func4(int rep){
int num4 = rep_val *4;
int i = 0;
for(i=0;i<num4;i++){
printf("WHAT?\n");
}
}
So, all I want to do is to spread each func1,2,3,4 so main.c can remain clear.
And following main.h , myfunc1.h, myfunc.c are what I did for func1.
main.h
int rep_val = 4;// myfunc1,2,3,4 will use this value so placed here
myfunc1.h
#include "main.h"
int num1 = rep_val*2;//rep_val = 4
int func1();
myfunc1.cpp
#include <myfunc1.h>
int func1(){
int i = 0;
for(i=0;i<num1;i++){
printf("HI!\n");
}
}
And the error was
D:\DEV\DEV C\Header_practice\main.h [Error] redefinition of 'int rep_val'
What am I missing? I've looked for several relevant queries and still can't find solution. Any word from you will be appriciated. Thanks!
Nam Jeongkuk