multifile const global varuble

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
/****************************** int main **************************************/
     const int ROWS=20;
     
     #include "header.h"

int main();
{
    invMenu();
    return 0;
}


/****************************** header.h **************************************/

#ifndef HEADER_H
#define HEADER_H

   #include <iostream>
   #include "invmenu.h"
   
   void invMenu();

   extern char bookTitle[ROWS][51];


#endif

/******************************* invmenu **************************************/
void invMenu()
{
     // extern const int ROWS; //not working ether.
     char bookTitle[ROWS][51]; // "error" 'ROWS' was not declared in this scope
     return; 
}


next to last line is error why i declared in mainmenu.cpp
extern const int ROWS;
If invmenu is a separate translation unit when it knows nothing about the name ROWS.
Also I do not see any sense in declaring

extern char bookTitle[ROWS][51];

in the header because inside function void invMenu() a local array with the same name is used.
Last edited on
how do i use one global constant on muti-file program. i have a lot more functions. i just wanted to keep it basic. right now i am researching #define but i cant figure out how to make it global ether. i might have to create another header file with all globals and call in nessisary functions.
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
/****************************** int main **************************************/
     #define ROWS 20;
     #include "header.h"

int main();
{
    invMenu();
    return 0;
}


/****************************** header.h **************************************/

#ifndef HEADER_H
#define HEADER_H

   #include <iostream>
   #include "invmenu.h"
   
   void invMenu();

   extern char bookTitle[ROWS][51];


#endif

/******************************* invmenu **************************************/
void invMenu()
{
     char bookTitle[ROWS][51]; // 'ROWS' was not declared in this scope
     return; 
}
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
/****************************** int main **************************************/

#include "header.h"

int main()
{
    invMenu();
    return 0;
}


/****************************** header.h **************************************/

#ifndef HEADER_H
#define HEADER_H

#include <iostream>



void invMenu();

const int ROWS = 20;

extern char bookTitle[ROWS][51];


#endif

/******************************* invmenu **************************************/
#include "header.h"

char bookTitle[ROWS][51];

void invMenu()
{
     return; 
}

Last edited on

What is "invmenu.h"?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef INVMENU_H
#define INVMENU_H

   #include<iostream>
   #include<iomanip>
   #include<fstream>
   
   const int ROWS_1D = 21;
   const int ROWS_2D = 20;

   void invMenu();
   void lookUpBook();
   void addBook();
   void editBook();
   void deleteBook();

#endif 
figured it out
const unsigned int ROWS_2D = 20;
i knew it was calling it more than once. know i know the "unsigned" prevents a multiple decloration.
:):):):):):):):) HAPPY!!!!!! :):):):):):):):)
*>cheers<*
Topic archived. No new replies allowed.