473,385 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Using constant variables in header file

Hi,

I have one constant variable and want to use it in two files. I put it in
the header file and then include the header file. The compiler always say
"error C2370: 'arraysize' : redefinition; different storage class". What
shall I do? Files are listed below. I'm using Visual C++ 6.0

By the way, how to use STL (vector , for example) to create a
multi-dimension array?

Many thanks.

Karen
---------------------------------
my header file - consts.h:
const int arraysize = 15;

my sources file 1 - op1.cpp:

#include consts.h
char array1[arraysize];

my source file 2 - op2.cpp:

#include consts.h
char array2[arraysize];


Jul 19 '05 #1
7 28923
"Karen" <Ka********@yahoo.com> wrote...
I have one constant variable and want to use it in two files. I put it in
the header file and then include the header file. The compiler always say
"error C2370: 'arraysize' : redefinition; different storage class". What
shall I do? Files are listed below. I'm using Visual C++ 6.0

By the way, how to use STL (vector , for example) to create a
multi-dimension array?

Many thanks.

Karen
---------------------------------
my header file - consts.h:
const int arraysize = 15;

my sources file 1 - op1.cpp:

#include consts.h
char array1[arraysize];

my source file 2 - op2.cpp:

#include consts.h
char array2[arraysize];


The only error I can see in those two translation units is that
the #include directory doesn't have the header name in either
double quotes or angle brackets. Beyond that, everything should
be fine.

Perhaps you should post the actual code that produces the error.

To create a multidimensional array from std::vector, create
a vector of vectors [of vectors ...] of the needed type:

std::vector<std::vector< ... <int> ... > > mda;

Don't forget to resize the mda. Or you could initialise it
with needed sizes for each dimension.

Victor
Jul 19 '05 #2
"Karen" <Ka********@yahoo.com> wrote...
I have one constant variable and want to use it in two files. I put it in
the header file and then include the header file. The compiler always say
"error C2370: 'arraysize' : redefinition; different storage class". What
shall I do? Files are listed below. I'm using Visual C++ 6.0

By the way, how to use STL (vector , for example) to create a
multi-dimension array?

Many thanks.

Karen
---------------------------------
my header file - consts.h:
const int arraysize = 15;

my sources file 1 - op1.cpp:

#include consts.h
char array1[arraysize];

my source file 2 - op2.cpp:

#include consts.h
char array2[arraysize];


The only error I can see in those two translation units is that
the #include directory doesn't have the header name in either
double quotes or angle brackets. Beyond that, everything should
be fine.

Perhaps you should post the actual code that produces the error.

To create a multidimensional array from std::vector, create
a vector of vectors [of vectors ...] of the needed type:

std::vector<std::vector< ... <int> ... > > mda;

Don't forget to resize the mda. Or you could initialise it
with needed sizes for each dimension.

Victor
Jul 19 '05 #3
Karen <Ka********@yahoo.com> writes
I have one constant variable and want to use it in two files. I put it in
the header file and then include the header file. The compiler always say
"error C2370: 'arraysize' : redefinition; different storage class". What
shall I do? Files are listed below. I'm using Visual C++ 6.0 ---------------------------------
my header file - consts.h:
const int arraysize = 15;


should that be:
static const int arraysize = 15;

By omitting the 'static' keyword, you're defining a variable with
external linkage. You're only allowed to do this once.

--
Simon Elliott
http://www.ctsn.co.uk/


Jul 19 '05 #4
"Simon Elliott" <si***@nospam.demon.co.uk> wrote...
Karen <Ka********@yahoo.com> writes
I have one constant variable and want to use it in two files. I put it in
the header file and then include the header file. The compiler always say
"error C2370: 'arraysize' : redefinition; different storage class". What
shall I do? Files are listed below. I'm using Visual C++ 6.0
---------------------------------
my header file - consts.h:
const int arraysize = 15;


should that be:
static const int arraysize = 15;


Doesn't have to.

By omitting the 'static' keyword, you're defining a variable with
external linkage. You're only allowed to do this once.


See 3.5/3. 'const' objects have internal linkage if _not_ declared
'extern' and _not_ previously declared to have external linkage.

Victor
Jul 19 '05 #5
Karen wrote:
Hi,

I have one constant variable and want to use it in two files. I put it in
the header file and then include the header file. The compiler always say
"error C2370: 'arraysize' : redefinition; different storage class". What
shall I do? Files are listed below. I'm using Visual C++ 6.0

By the way, how to use STL (vector , for example) to create a
multi-dimension array?

Many thanks.

Karen
---------------------------------
my header file - consts.h:
const int arraysize = 15;

my sources file 1 - op1.cpp:

#include consts.h
#include "consts.h" // you forgot the quotes
char array1[arraysize];

my source file 2 - op2.cpp:

#include consts.h #include "consts.h"
char array2[arraysize];


The reason for the multiple definition error is that you need what's
referred to as an include guard:

//////////////////////////
// consts.h

#ifndef CONSTS_H_INCLUDED
#define CONSTS_H_INCLUDED

const int arraysize = 15;

#endif // include guard
/////////////////////////

What that does is ensures that the header file is only included once in
each compilation unit. Try that in your program, and then read up on
include guards in your favorite reference book. There may also be
something in the newsgroup's FAQ.

- Adam

--
Reverse domain name to reply.

Jul 19 '05 #6
Karen wrote:
<snip>

By the way, how to use STL (vector , for example) to create a
multi-dimension array?

<snip>

Something like this:

//////////////////////////////////////////////////////////////
#include <vector>
#include <iostream>
#include <iterator>

int
main()
{
typedef std::vector< std::vector<int> > table_t;

table_t table;

for (int i = 1; i <= 9; ++i)
{
std::vector<int> row;

for (int j = 1; j <= 9; ++j)
{
row.push_back(i * j);
}

table.push_back(row);
}

int factor1 = 6, factor2 = 8;
std::cout << factor1 << " times " << factor2 << " is "
<< table[factor1 - 1][factor2 - 1]
<< "\n\n";

for (table_t::const_iterator r = table.begin();
r != table.end();
++r)
{
copy(r->begin(), r->end(),
std::ostream_iterator<int>(std::cout, "\t"));
std::cout << '\n';
}

return 0;
}
//////////////////////////////////////////////////////////////

- Adam
--
Reverse domain name to reply.

Jul 19 '05 #7
In article <bx*****************@news.uswest.net>,
Adam Fineman <af******@retupmoc.org> wrote:
[snip]
typedef std::vector< std::vector<int> > table_t;

table_t table;

for (int i = 1; i <= 9; ++i)
{
std::vector<int> row;

for (int j = 1; j <= 9; ++j)
{
row.push_back(i * j);
}

table.push_back(row);
}


Or if you know the number of rows and columns in advance, you can
construct the vector of vectors with that size:

vector<vector<int> > table (numRows, vector<int>(numCols))

then fill it up just like you would an array.

for (int row = 0; row < numRows; ++row)
{
for (col = 0; col < numCols; ++col)
{
table[row][col] = row * col;
}
}

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Steven Scaife | last post by:
I have decided to re-write the intranet site i created over a year ago. The coding is pretty awful and hard to read cos I made the mistake of not putting comments in or putting crappy comments in...
4
by: August1 | last post by:
A handful of articles have been posted requesting information on how to use these functions in addition to the time() function as the seed to generate unique groups (sets) of numbers - each group...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
5
by: Timo | last post by:
I am having a problem with some preprocessor constant value checking: header.h: #define MY_CONSTANT 1 file.c #if MY_CONSTANTTT == 1 // <- note the typo!! // dostuff...
6
by: Prasad | last post by:
Where are the const variables actually stored in memory. I mean If they are stored in data segment(external & static variables)or stack segment(local) how the compiler knows that it is read only...
1
by: Xiangliang Meng | last post by:
Hi, all. Recently, I find there is a way in our project to maintain a global set in many files by using preprocessing directives. I'm wondering if we could find a better method for this. Many...
15
by: Scott | last post by:
Hi All, I have the following C code in a header file, outside of any functions: const float X = 50; const float Y = 100 * X; But, when compiling, I get an error: initializer element is...
12
by: mantrid | last post by:
Im trying to move a file but am having luck my code is below. The temp and target paths are valid as they echo correctly. but I cant get the copy() function to work, or the rename() function ...
9
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.