473,396 Members | 1,834 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,396 software developers and data experts.

declare array of objects in header file


Hello, I've got a class Lattice which is declared like that:

// file: lattice.h

class Lattice:public EventChooser{
public:
Lattice(LatticeParameters* params);
virtual ~Lattice();
// (...snip...)

private:
// How do I declare this member array in the lattice.h file?
// The following doesn't seem to work
LatticeSite** lattice; // does not compile
};
// file: lattice.cpp

Lattice::Lattice(LatticeParameters* params){

// Initializing 3D lattice
LatticeSite lattice[Ni][Nj][Nk];

for(int i=0; i<Ni; i++)
for(int j=0; j<Nj; j++)
for(int k=0; k<Nk; k++)
lattice[i][j][k].setValues(i,j,k,0);
}

The class LatticeSite has a default constructor.
So I don't know how to declare "lattice" which is an array of LatticeSite
objects in the file lattice.h.

Any ideas? Thanks Phil
Jul 19 '05 #1
5 12550

"Philipp" <_N******************@hotmail.com> wrote in message
news:3f********@epflnews.epfl.ch...

Hello, I've got a class Lattice which is declared like that:

// file: lattice.h

class Lattice:public EventChooser{
public:
Lattice(LatticeParameters* params);
virtual ~Lattice();
// (...snip...)

private:
// How do I declare this member array in the lattice.h file?
// The following doesn't seem to work
LatticeSite** lattice; // does not compile
};


Is LatticeSite declared in this header file? If not then remember to
#include it
BTW LatticeSite** is not setting an array, this is a pointer to a pointer to
a LatticeSite object. An array would be declared like
LatticeSite lattice[10];
However, if you dont know how many elements are required at compile time
then you will have to use a pointer and then initialise it using new, e.g.

LatticeSite *lattice; // declared in class definition

LatticeSite = new lattice[x]; // declared elsewhere (i suggest
LatticeSite::init)

HTH
Allan
Jul 19 '05 #2

">
LatticeSite = new lattice[x]; // declared elsewhere (i suggest
LatticeSite::init)


I take it you mean:

lattice = new LatticeSite[x]; ?
And I have no idea what you mean by suggesting he "declare" it in a
(static?) init function...? (Are you referring to 'x' being assigned a
value elsewhere?)
-Howard
Jul 19 '05 #3
Hmm
I guess i can't allocate a static array with a variable as size.

int N=2;
int a[N];

won't work. Ok I'll to do do it dynamically then :-)


Jul 19 '05 #4

"Howard" <al*****@hotmail.com> wrote in message
news:bm********@dispatch.concentric.net...

">
LatticeSite = new lattice[x]; // declared elsewhere (i suggest
LatticeSite::init)

I take it you mean:

lattice = new LatticeSite[x]; ?


Yes I do - need some sleep!


And I have no idea what you mean by suggesting he "declare" it in a
(static?) init function...? (Are you referring to 'x' being assigned a
value elsewhere?)


I meant x was declared somewhere so that the memory is allocated
dynamically, but even better:

// Lattice.h
#include "LatticeSite.h"

class Lattice:public EventChooser
{
public:
bool Init(int xiNumElements);
....
private:
LatticeSite* mLattice;
};

//Lattice.cpp
bool Lattice::Init(int xiNumElements)
{
if ( (mLattice = new LatticeSite[xiNumElements]) == NULL)
return true; // couldnt allocate the memory

return false; // no problems :o)
}

I hope thats clear
Allan
Jul 19 '05 #5

"Philipp" <_N******************@hotmail.com> wrote in message news:3f********@epflnews.epfl.ch...
Hmm
I guess i can't allocate a static array with a variable as size.

int N=2;
int a[N];

You can't declare an array anywhere with a given size that is not a constant expression.

vector<int> a(N);

would probably work fine for you.
Jul 19 '05 #6

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

Similar topics

2
by: Secondpost | last post by:
I need to use a calender object with a dynamic form i.e. when a new row of fields is created using Javascript a calender button appears aswell. The problem is if I need to declare the following...
1
by: hepp | last post by:
I have a header file that contains some constans, and the following lines are included in it: const int a = {1}; const int b = {2}; const int* c = {a, b}; My code compiles, but the linker...
15
by: Geoff Cox | last post by:
Hello, Can I separately declare and initialize a string array? How and where would I do it in the code below? It was created using Visual C++ 2005 Express Beta 2 ... In C# I would have ...
8
by: ben | last post by:
hello there, oh dear, oh dear. here's a non global array of strings: char *chararray = { "abc", "defgh", "ijklmop" }; how do i do that so chararray is global? what goes in a .h file and...
8
by: redefined.horizons | last post by:
I would like to have an array declaration where the size of the array is dependent on a variable. Something like this: /* Store the desired size of the array in a variable named "array_size". */...
4
by: Rui.Hu719 | last post by:
Hi, All: I read the following passage from a book: "There are three exceptions to the rule that headers should not contain definitions: classes, const objects whose value is known at compile...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
3
by: johnmmcparland | last post by:
Hi all, I would like to have a static constant array inside a class definition which would contain the number of days in each month (I am writing a Date class as an exercise). However my...
10
by: Tammy | last post by:
Hello all, I am wondering what is the best way to declare a struct to be used in other c and c++ files. Such as for a C API that will be used by others. 1. Declaring the typedef and the...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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...
0
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...
0
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,...

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.