473,785 Members | 2,746 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

array as member of class

Hi! I've organized my code in different files - the declarations of
global variables and methods are contained in a file global.hh (for
compilation via makefile under Linux). In this file I included the
following:

extern const int dim; //dimension of the array

class Particle{
public:
double x[dim]; //particle position
};
Since dim is defined in another file (where main() is located), the code
does not compile. Does anyone have an idea how this could be fixed? I've
consulted several books, but this doesn't seem to be a common problem
(or I have the wrong books).

Thanks in advance for any advice!

Florian

Aug 8 '07 #1
8 2264
Florian Bürzle wrote:
Hi! I've organized my code in different files - the declarations of
global variables and methods are contained in a file global.hh (for
compilation via makefile under Linux). In this file I included the
following:

extern const int dim; //dimension of the array

class Particle{
public:
double x[dim]; //particle position
};
Since dim is defined in another file (where main() is located), the code
does not compile. Does anyone have an idea how this could be fixed? I've
consulted several books, but this doesn't seem to be a common problem
(or I have the wrong books).
The array size has to be a compile time constant, it has to be known
when each translation unit that include the header is compiled.

You can either put the value in the header (say a static const unsigned
member), or use a dynamic array.

--
Ian Collins.
Aug 8 '07 #2
"Florian Bürzle" <fb******@gmx.d ewrote in message
news:46******** **************@ newsspool2.arco r-online.net...
Hi! I've organized my code in different files - the declarations of
global variables and methods are contained in a file global.hh (for
compilation via makefile under Linux). In this file I included the
following:

extern const int dim; //dimension of the array

class Particle{
public:
double x[dim]; //particle position
};
Since dim is defined in another file (where main() is located), the code
does not compile. Does anyone have an idea how this could be fixed? I've
consulted several books, but this doesn't seem to be a common problem
(or I have the wrong books).
The compiler has to know what dim is when it gets to this line of code. It
needs to be a constant, but also knowable at compile time. Even if you
declared dim before you included the header, that would probably only work
in one of your compilation units. external linkage is that, linkage, not
compiling.

One solution: use a std::vector<dou bleisntead of an array. Have
Particle's constructor size it to fit (even using the global if you
absolutely must, but it would be better, IMO, to pass the value through the
constructor's paramater list).
Aug 8 '07 #3
Florian Bürzle wrote:
Hi! I've organized my code in different files - the declarations of
global variables and methods are contained in a file global.hh (for
compilation via makefile under Linux). In this file I included the
following:

extern const int dim; //dimension of the array

class Particle{
public:
double x[dim]; //particle position
};
Since dim is defined in another file (where main() is located), the code
does not compile. Does anyone have an idea how this could be fixed? I've
consulted several books, but this doesn't seem to be a common problem
(or I have the wrong books).

Thanks in advance for any advice!
Two options.
a) Place the "const int dim = N" in the header.
b) make Particle::x a std::vector
Aug 8 '07 #4
Ian Collins wrote:
The array size has to be a compile time constant, it has to be known
when each translation unit that include the header is compiled.

You can either put the value in the header (say a static const unsigned
member), or use a dynamic array.
Thanks for the quick reply. Can you give me a hint how to implement such
a dynamic array in this case? (Sorry if this is trivial, but I've never
used dynamic arrays).

Thanks!

Florian
Aug 8 '07 #5
"Florian Bürzle" <fb******@gmx.d ewrote in message
news:46******** *************** @newsspool1.arc or-online.net...
Ian Collins wrote:
>The array size has to be a compile time constant, it has to be known
when each translation unit that include the header is compiled.

You can either put the value in the header (say a static const unsigned
member), or use a dynamic array.

Thanks for the quick reply. Can you give me a hint how to implement such
a dynamic array in this case? (Sorry if this is trivial, but I've never
used dynamic arrays).
Pick one. My preference is Foo.

#include <iostream>
#include <vector>

extern const int dim; //dimension of the array

const int dim = 10;

class Foo
{
public:
Foo( const int Size )
{
x.resize( Size );
}
std::vector<dou blex;
};

class Foo2
{
public:
Foo2()
{
x.resize( dim );
}
std::vector<dou blex;
};

class Bar
{
public:
Bar( const int Size ) { x = new double[ Size ]; }
~Bar() { delete[] x; }
double* x;
};

class Bar2
{
public:
Bar2() { x = new double[ dim ]; }
~Bar2() { delete[] x; }
double* x;
};

int main()
{
Foo foo( dim );
Foo2 foo2;
Bar bar( dim );
Bar2 bar2;

return 0;
}
Aug 8 '07 #6
Florian Bürzle wrote:
Ian Collins wrote:
>The array size has to be a compile time constant, it has to be known
when each translation unit that include the header is compiled.

You can either put the value in the header (say a static const unsigned
member), or use a dynamic array.

Thanks for the quick reply. Can you give me a hint how to implement such
a dynamic array in this case? (Sorry if this is trivial, but I've never
used dynamic arrays).
Here is some example code.

------------
#include <vector>

// VectDim is basically a vector with a default dimension
// given by a const int reference
template <typename T, const int & D>
class VectDim : public std::vector<T>
{
public:
VectDim() : std::vector<T>( D )
{
}
};

extern const int dim;

class Particle{
public:
VectDim<double, dimx; //particle position
};
const int dim = 4;

#include <iostream>

int main()
{
Particle p;

std::cout << p.x.size() << "\n";
}
------------
Aug 8 '07 #7

Jim Langston <ta*******@rock etmail.comwrote in message...
"Florian Bürzle" <fb******@gmx.d ewrote in message...
Thanks for the quick reply. Can you give me a hint how to implement such
a dynamic array in this case? (Sorry if this is trivial, but I've never
used dynamic arrays).

Pick one. My preference is Foo.

#include <iostream>
#include <vector>

extern const int dim; // dimension of the array
const int dim = 10;

class Foo
{
public:
Foo( const int Size )
{
x.resize( Size );
}
std::vector<dou blex;
};
I prefer 'initialization lists':

class Foo{ public:
Foo( size_t const Size ) : x( Size, 3.14 ){} // OP, note the colon
Foo() : x( dim ){} // 'dim' doubles inited to zero (Foo2 below)
std::vector<dou blex;
};
/* - main() or ? -
Foo aFoo( 7 );
std::cout<<" aFoo.x.at(2)="< <aFoo.x.at(2)<< std::endl;
// out: aFoo.x.at(2)=3. 140000
*/
class Foo2{ public: // shortened to *my* style for NG post
Foo2(){ x.resize( dim ); }
std::vector<dou blex;
};

class Bar{ public:
Bar( const int Size ) { x = new double[ Size ]; }
~Bar() { delete[] x; }
double* x;
};
class Bar{ public:
Bar( size_t const Size ) : x( new double[ Size ] ){}
~Bar() { delete[] x; }
double *x;
};
// 'function level try block' for Ctor not shown.

// Why 'size_t' (unsigned integer type)?
class Bar{ public:
// Bar( int const Size ) : x( new double[ Size ] ){}
// use: Bar aBar( -1 ); runtime-error: bad_alloc

Bar( size_t const Size ) : x( new double[ Size ] ){}
// use: Bar aBar( -1 ); compile-warning
// [Warning] passing negative value `-1'
// for argument 1 of `Bar::Bar(unsig ned int)'
// == a HUGE size

~Bar(){ delete[] x; }
double *x;
};

[ snip other examples ]

OP: Jim was keeping it simple so not to confuse you. Look over his post
before you tackle the 'init list' modifications I've shown.
The 'initialization lists; are the prefered way to do it, when you can
(sometimes you must use the body of the constructor (like to set init values
in array[])).
More questions? No problem, ask away.

--
Bob R
POVrookie
Aug 8 '07 #8
Thanks to all for your help, especially for the code examples!

Florian
Aug 10 '07 #9

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

Similar topics

4
2300
by: Avner Flesch | last post by:
Hi, Do you know how can I intialize an array member: for example class A { public: A(int x); };
10
6695
by: Adam Warner | last post by:
Hi all, With this structure that records the length of an array of pointers as its first member: struct array { ptrdiff_t length; void *ptr; };
2
2997
by: DevarajA | last post by:
Can someone help me understand what flexible array members exactly are, how they behave and how could them be implemented by a i386? Also I didn't understand the two exceptions that the standards talks about (when talking about ignoring the flexible array member). Please help me. -- Devaraja (Xdevaraja87^gmail^c0mX) Linux Registerd User #338167 http://counter.li.org
2
2932
by: jccorreu | last post by:
I'm taking in data from multiple files that represents complex numbers for charateristics of different elements. I begin with arrays of doubles and interpolate to get standard values in real and imaginary portions. To this end I have some dynamic arrays of doubles to hold the standardized individual rael and imaginary parts. double *ANreal, *ANimag, *BNreal, *BNimag, *CNreal, *CNimag; ANreal = new double; ANimag = new double;
2
2273
by: tobiasoed | last post by:
Hello, I want to do something similar to this: #include <stdlib.h> struct coord { double val; double err; };
6
1458
by: faisalee | last post by:
Hi Guys, I have a question. Here goes: class clsManager { public: int x; int y; }
5
4749
by: Lyle Avery | last post by:
Hello guys, Look at this in c++ file: class T { public: char c; char ca; };
3
3892
by: Hallvard B Furuseth | last post by:
to find the required alignment of a struct, I've used #include <stddef.h> struct Align_helper { char dummy; struct S align; }; enum { S_alignment = offsetof(struct Align_helper, align) };
2
7699
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
5
1611
by: vijayphan | last post by:
Hi I am new to php. I have a query. I have a class which has a array member variable list1 and list2. Am trying to update it in a function and not able to. Here is the class. Please help me. <?php class simple { var $list1;
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10319
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10087
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9947
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.