473,706 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(Lattice Parameters* 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::Lattic e(LatticeParame ters* 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 12569

"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(Lattice Parameters* 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::in it)

HTH
Allan
Jul 19 '05 #2

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


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*****@hotmai l.com> wrote in message
news:bm******** @dispatch.conce ntric.net...

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

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 "LatticeSit e.h"

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

//Lattice.cpp
bool Lattice::Init(i nt 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
6405
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 code for the calendar script to work: var cal6 = new calendar1(document.forms.elements); cal6.year_scroll = true; cal6.time_comp = false; where the # 6 will change as the user adds a new field (by clicking a button).
1
289
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 complains about that "c" is multiple declared. It seems to have been defined in all source files that included this header file. Why? It only seems to be a problems when
15
5319
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 private string myArray;
8
1735
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 then what goes in a .c file's function? i've just tried a whole load of
8
10164
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". */ unsigned short int array_size = 25; /*Declare an array named "numbers" using the variable initialized above. */ unsigned short int numbers;
4
3303
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 time, and inline functions are all defined in headers. " Can someone explain to me why some of the const objects must be defined in the header file?
23
7409
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 in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
3
20873
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 attempts so far have been unsuccessful. Take this Test class as an example // test.hpp
10
2474
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 struct in the header file and including this file in all source files that need it? For example: mystruct.h
0
8779
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
8695
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
9282
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...
0
9154
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7900
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
4445
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4708
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3142
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
2
2499
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.