473,772 Members | 2,513 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Static arrays of objects in C++ in other classes.

Jim
Hi,
I'm trying to build a simple data table for all my classes to use. I
don't want to use a vector. The data is stored in class freqoffset

class freqoffset
{
public:
freqoffset():of fset(0),strengt h(0){};
int set(double offset,double strength);
double getOff() const{return offset;};
double getStr() const{return strength;};
private:
double offset,strength ;
};

int freqoffset::set (double off,double str)
{
offset=off;
strength=str;
return 0;
}

And the class with the data in (cut down significantly) is;
class nh3fitting
{
private:
static const int sn1=18;
static const int sn2=21;
static freqoffset s1[sn1];
static freqoffset s2[sn2];
public:
nh3fitting(doub le cr, double cd, int n, double sd); //generates an
array and populates it with x values

static int loadArrays();

};

int nh3fitting::loa dArrays()
{
s1[0].set( -1.56893, 0.0741);
s1[1].set(-1.526658 , 0.1481);
s1[2].set(-0.623335 , 0.0926);
s1[3].set( -0.590375, 0.1667);
s1[4].set(-0.581021, 0.0185);
s1[5].set(-0.036389 , 0.0370);
s1[6].set(-0.025453 , 0.0185);
s1[7].set(-0.024583, 0.0333);
s1[8].set(-0.015196, 0.3);
s1[9].set(0.005941, 0.0185);
s1[10].set(0.010463, 0.4667);
s1[11].set(0.016835, 0.0926);
s1[12].set(0.019832, 0.0333);
s1[13].set(0.571708, 0.0926);
s1[14].set(0.582719, 0.0185);
s1[15].set(0.617689 , 0.1667);
s1[16].set(1.53405, 0.0741);
s1[17].set(1.545159, 0.1481);

s2[0].set(-2.099033 ,0.0042 );
s2[1].set(-2.058267 , 0.0377);
s2[2].set( -2.053459, 0.0209);
s2[3].set(-1.297087, 0.0372);
s2[4].set(-1.296079,0.026 );
s2[5].set(-1.255384, 0.0019);
s2[6].set(-0.044498, 0.0209);
s2[7].set(-0.041806, 0.0106);
s2[8].set(-0.041456, 0.0116);
s2[9].set(-0.001031, 0.1465);
s2[10].set(0.000309, 0.4997);
s2[11].set(0.001054, 0.2674);
s2[12].set(0.039736, 0.0106);
s2[13].set(0.042055, 0.0116);
s2[14].set(0.046621 , 0.0209);
s2[15].set(1.254559, 0.0019);
s2[16].set(1.295363, 0.026);
s2[17].set(1.296295, 0.0377);
s2[18].set(2.053476, 0.0209);
s2[19].set(2.058256, 0.0377);
s2[20].set(2.099022, 0.0042);

return 0;
}

Problem is that I get the following error in linking when I try and
compile it.
g++ -I/usr/include/gsl/ -L/usr/lib/ -c nh3fit.cpp
g++ -I/usr/include/gsl/ -L/usr/lib/ -lgsl -lgslcblas nh3fit.o -o
nh3fit
nh3fit.o: In function `nh3fitting::lo adArrays()':
nh3fit.cpp:(.te xt+0xb7): undefined reference to `nh3fitting::s1 '
nh3fit.cpp:(.te xt+0xd9): undefined reference to `nh3fitting::s1 '
nh3fit.cpp:(.te xt+0xfb): undefined reference to `nh3fitting::s1 '
nh3fit.cpp:(.te xt+0x11d): undefined reference to `nh3fitting::s1 '
nh3fit.cpp:(.te xt+0x13f): undefined reference to `nh3fitting::s1 '

and so on, but I've no way of solving this without changing the
structure of the code.

Any help would be greatly appreciated.
Jim
Jul 9 '08 #1
7 1672
Jim wrote:
Hi,
I'm trying to build a simple data table for all my classes to use. I
don't want to use a vector. The data is stored in class freqoffset

class freqoffset
{
public:
freqoffset():of fset(0),strengt h(0){};
int set(double offset,double strength);
double getOff() const{return offset;};
double getStr() const{return strength;};
private:
double offset,strength ;
};

int freqoffset::set (double off,double str)
{
offset=off;
strength=str;
return 0;
}

And the class with the data in (cut down significantly) is;
class nh3fitting
{
private:
static const int sn1=18;
static const int sn2=21;
static freqoffset s1[sn1];
static freqoffset s2[sn2];
public:
nh3fitting(doub le cr, double cd, int n, double sd); //generates an
array and populates it with x values

static int loadArrays();

};

int nh3fitting::loa dArrays()
{
s1[0].set( -1.56893, 0.0741);
s1[1].set(-1.526658 , 0.1481);
s1[2].set(-0.623335 , 0.0926);
s1[3].set( -0.590375, 0.1667);
s1[4].set(-0.581021, 0.0185);
s1[5].set(-0.036389 , 0.0370);
s1[6].set(-0.025453 , 0.0185);
s1[7].set(-0.024583, 0.0333);
s1[8].set(-0.015196, 0.3);
s1[9].set(0.005941, 0.0185);
s1[10].set(0.010463, 0.4667);
s1[11].set(0.016835, 0.0926);
s1[12].set(0.019832, 0.0333);
s1[13].set(0.571708, 0.0926);
s1[14].set(0.582719, 0.0185);
s1[15].set(0.617689 , 0.1667);
s1[16].set(1.53405, 0.0741);
s1[17].set(1.545159, 0.1481);

s2[0].set(-2.099033 ,0.0042 );
s2[1].set(-2.058267 , 0.0377);
s2[2].set( -2.053459, 0.0209);
s2[3].set(-1.297087, 0.0372);
s2[4].set(-1.296079,0.026 );
s2[5].set(-1.255384, 0.0019);
s2[6].set(-0.044498, 0.0209);
s2[7].set(-0.041806, 0.0106);
s2[8].set(-0.041456, 0.0116);
s2[9].set(-0.001031, 0.1465);
s2[10].set(0.000309, 0.4997);
s2[11].set(0.001054, 0.2674);
s2[12].set(0.039736, 0.0106);
s2[13].set(0.042055, 0.0116);
s2[14].set(0.046621 , 0.0209);
s2[15].set(1.254559, 0.0019);
s2[16].set(1.295363, 0.026);
s2[17].set(1.296295, 0.0377);
s2[18].set(2.053476, 0.0209);
s2[19].set(2.058256, 0.0377);
s2[20].set(2.099022, 0.0042);

return 0;
}

Problem is that I get the following error in linking when I try and
compile it.
g++ -I/usr/include/gsl/ -L/usr/lib/ -c nh3fit.cpp
g++ -I/usr/include/gsl/ -L/usr/lib/ -lgsl -lgslcblas nh3fit.o -o
nh3fit
nh3fit.o: In function `nh3fitting::lo adArrays()':
nh3fit.cpp:(.te xt+0xb7): undefined reference to `nh3fitting::s1 '
nh3fit.cpp:(.te xt+0xd9): undefined reference to `nh3fitting::s1 '
nh3fit.cpp:(.te xt+0xfb): undefined reference to `nh3fitting::s1 '
nh3fit.cpp:(.te xt+0x11d): undefined reference to `nh3fitting::s1 '
nh3fit.cpp:(.te xt+0x13f): undefined reference to `nh3fitting::s1 '

and so on, but I've no way of solving this without changing the
structure of the code.
Correct, you don't. You need to _define_ any static data member in your
class if it's used outside of the class. FAQ 10.11. Always read the
FAQ before posting.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 9 '08 #2
Jim
On Jul 9, 5:42 pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
Jim wrote:
Hi,
I'm trying to build a simple data table for all my classes to use. I
don't want to use a vector. The data is stored in class freqoffset
class freqoffset
{
public:
freqoffset():of fset(0),strengt h(0){};
int set(double offset,double strength);
double getOff() const{return offset;};
double getStr() const{return strength;};
private:
double offset,strength ;
};
int freqoffset::set (double off,double str)
{
offset=off;
strength=str;
return 0;
}
And the class with the data in (cut down significantly) is;
class nh3fitting
{
private:
static const int sn1=18;
static const int sn2=21;
static freqoffset s1[sn1];
static freqoffset s2[sn2];
public:
nh3fitting(doub le cr, double cd, int n, double sd); //generates an
array and populates it with x values
static int loadArrays();
};
int nh3fitting::loa dArrays()
{
s1[0].set( -1.56893, 0.0741);
s1[1].set(-1.526658 , 0.1481);
s1[2].set(-0.623335 , 0.0926);
s1[3].set( -0.590375, 0.1667);
s1[4].set(-0.581021, 0.0185);
s1[5].set(-0.036389 , 0.0370);
s1[6].set(-0.025453 , 0.0185);
s1[7].set(-0.024583, 0.0333);
s1[8].set(-0.015196, 0.3);
s1[9].set(0.005941, 0.0185);
s1[10].set(0.010463, 0.4667);
s1[11].set(0.016835, 0.0926);
s1[12].set(0.019832, 0.0333);
s1[13].set(0.571708, 0.0926);
s1[14].set(0.582719, 0.0185);
s1[15].set(0.617689 , 0.1667);
s1[16].set(1.53405, 0.0741);
s1[17].set(1.545159, 0.1481);
s2[0].set(-2.099033 ,0.0042 );
s2[1].set(-2.058267 , 0.0377);
s2[2].set( -2.053459, 0.0209);
s2[3].set(-1.297087, 0.0372);
s2[4].set(-1.296079,0.026 );
s2[5].set(-1.255384, 0.0019);
s2[6].set(-0.044498, 0.0209);
s2[7].set(-0.041806, 0.0106);
s2[8].set(-0.041456, 0.0116);
s2[9].set(-0.001031, 0.1465);
s2[10].set(0.000309, 0.4997);
s2[11].set(0.001054, 0.2674);
s2[12].set(0.039736, 0.0106);
s2[13].set(0.042055, 0.0116);
s2[14].set(0.046621 , 0.0209);
s2[15].set(1.254559, 0.0019);
s2[16].set(1.295363, 0.026);
s2[17].set(1.296295, 0.0377);
s2[18].set(2.053476, 0.0209);
s2[19].set(2.058256, 0.0377);
s2[20].set(2.099022, 0.0042);
return 0;
}
Problem is that I get the following error in linking when I try and
compile it.
g++ -I/usr/include/gsl/ -L/usr/lib/ -c nh3fit.cpp
g++ -I/usr/include/gsl/ -L/usr/lib/ -lgsl -lgslcblas nh3fit.o -o
nh3fit
nh3fit.o: In function `nh3fitting::lo adArrays()':
nh3fit.cpp:(.te xt+0xb7): undefined reference to `nh3fitting::s1 '
nh3fit.cpp:(.te xt+0xd9): undefined reference to `nh3fitting::s1 '
nh3fit.cpp:(.te xt+0xfb): undefined reference to `nh3fitting::s1 '
nh3fit.cpp:(.te xt+0x11d): undefined reference to `nh3fitting::s1 '
nh3fit.cpp:(.te xt+0x13f): undefined reference to `nh3fitting::s1 '
and so on, but I've no way of solving this without changing the
structure of the code.

Correct, you don't. You need to _define_ any static data member in your
class if it's used outside of the class. FAQ 10.11. Always read the
FAQ before posting.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
s1 and s2 are only ever used in static members of that class. Read
10.11 but not really sure how to apply it to the case of creating
static arrays of user-defined classes. I would have thought that the
objects are defined, they've got a default constructor which
initialises each of them, so things would have been fine.
I've tried changing
static freqoffset s1[sn1];
static freqoffset s2[sn2];
to
static freqoffset s1;
static freqoffset s2;
and placing
freqoffset nh3fitting::s1[sn1];
freqoffset nh3fitting::s2[sn2];
outside the class definition, but can't get it to work.

Any advice?
Jim
Jul 9 '08 #3
Jim wrote:
[..]
s1 and s2 are only ever used in static members of that class.
It does not matter where outside of the class definition they are used.
They are used, they need to be defined.

Read
10.11 but not really sure how to apply it to the case of creating
static arrays of user-defined classes. I would have thought that the
objects are defined, they've got a default constructor which
initialises each of them, so things would have been fine.
I've tried changing
static freqoffset s1[sn1];
static freqoffset s2[sn2];
to
static freqoffset s1;
static freqoffset s2;
and placing
freqoffset nh3fitting::s1[sn1];
freqoffset nh3fitting::s2[sn2];
outside the class definition, but can't get it to work.

Any advice?
Yes, follow this example (which isn't supposed to link as written):

class foo {
static int array[42];
public:
static void bar();
};

// int foo::array[42] = {}; // uncomment to make it link

void foo::bar()
{
array[0] = 666;
}

int main()
{
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 9 '08 #4
Jim
On Jul 9, 6:30 pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
Jim wrote:
[..]
s1 and s2 are only ever used in static members of that class.

It does not matter where outside of the class definition they are used.
They are used, they need to be defined.
Read


10.11 but not really sure how to apply it to the case of creating
static arrays of user-defined classes. I would have thought that the
objects are defined, they've got a default constructor which
initialises each of them, so things would have been fine.
I've tried changing
static freqoffset s1[sn1];
static freqoffset s2[sn2];
to
static freqoffset s1;
static freqoffset s2;
and placing
freqoffset nh3fitting::s1[sn1];
freqoffset nh3fitting::s2[sn2];
outside the class definition, but can't get it to work.
Any advice?

Yes, follow this example (which isn't supposed to link as written):

class foo {
static int array[42];
public:
static void bar();
};

// int foo::array[42] = {}; // uncomment to make it link

void foo::bar()
{
array[0] = 666;
}

int main()
{
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Marvellous, I've not seen that notation used before, thanks!

On a related note, if you're willing to help?
In my header, I have a whole bunch of constants defined which are used
throughout the program in static methods, as follows;
class nh3fitting
{
private:
static const double t0=41.5;
static const double v11= 23694.495;
static const double v22= 23722.622;
};

v11 and v22 work, but t0 only works in some static methods but not
others. If I follow the FAQ and define it outside the header. i.e.
class nh3fitting
{
private:
static const double t0;
static const double v11= 23694.495;
static const double v22= 23722.622;
};
double const nh3fitting::t0= 41.5;

Everything works and compiles.
I'm using gcc. Do you have any idea why this might be?
Thank you very much,
Jim
Jul 9 '08 #5
Jim wrote:
On a related note, if you're willing to help?
In my header, I have a whole bunch of constants defined which are used
throughout the program in static methods, as follows;
class nh3fitting
{
private:
static const double t0=41.5;
static const double v11= 23694.495;
static const double v22= 23722.622;
};

v11 and v22 work, but t0 only works in some static methods but not
others. If I follow the FAQ and define it outside the header. i.e.
class nh3fitting
{
private:
static const double t0;
static const double v11= 23694.495;
static const double v22= 23722.622;
};
double const nh3fitting::t0= 41.5;

Everything works and compiles.
I'm using gcc. Do you have any idea why this might be?
Thank you very much,
Jim
You can only initialize data objects in a class declaration if they are
static constant integrals. I don't know why v11 and v22 are working for
you; as far as I know they should not.
Jul 9 '08 #6
Jim
On Jul 9, 6:50 pm, Noah Roberts <u...@example.n etwrote:
Jim wrote:
On a related note, if you're willing to help?
In my header, I have a whole bunch of constants defined which are used
throughout the program in static methods, as follows;
class nh3fitting
{
private:
static const double t0=41.5;
static const double v11= 23694.495;
static const double v22= 23722.622;
};
v11 and v22 work, but t0 only works in some static methods but not
others. If I follow the FAQ and define it outside the header. i.e.
class nh3fitting
{
private:
static const double t0;
static const double v11= 23694.495;
static const double v22= 23722.622;
};
double const nh3fitting::t0= 41.5;
Everything works and compiles.
I'm using gcc. Do you have any idea why this might be?
Thank you very much,
Jim

You can only initialize data objects in a class declaration if they are
static constant integrals. I don't know why v11 and v22 are working for
you; as far as I know they should not.
static constant integrals? Do you mean static const int? It's very odd
as I use t0 in three static methods and only get a linker error from
two.I've checked the code and it seems good.
Jim
Jul 10 '08 #7
On Wed, 09 Jul 2008 10:50:10 -0700, Noah Roberts wrote:
Jim wrote:
>On a related note, if you're willing to help? In my header, I have a
whole bunch of constants defined which are used throughout the program
in static methods, as follows; class nh3fitting
{
private:
static const double t0=41.5;
static const double v11= 23694.495;
static const double v22= 23722.622;
};

v11 and v22 work, but t0 only works in some static methods but not
others. If I follow the FAQ and define it outside the header. i.e.
class nh3fitting
{
private:
static const double t0;
static const double v11= 23694.495;
static const double v22= 23722.622;
};
double const nh3fitting::t0= 41.5;

Everything works and compiles.
I'm using gcc. Do you have any idea why this might be? Thank you very
much,
Jim

You can only initialize data objects in a class declaration if they are
static constant integrals. I don't know why v11 and v22 are working for
you; as far as I know they should not.
I've just checked my current gcc (4.3.1) and I find that without the
`-pedantic' flag it *will* accept non-integral static member
initialisation, even if -std=c++98 is specified (go figure...). With
`-pedantic' I get:

error: ISO C++ forbids initialization of member constant ‘x’ of non-
integral type ‘const double’

It is an unfortunate fact of life that most compilers seem to default to
non-standards compliance. I generally use gcc with (at least) `-std=c++98
-pedantic -Wall -Werror -Wextra' (I recall that James Kanze recently
posted a rather more extensive list of options to cajole gcc into strict
standards compliance).

--
Lionel B
Jul 10 '08 #8

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

Similar topics

21
3937
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to "browse" it). So I expected that when I run this program, I get both c1.A and c2.A pointing to the same address, and changing c1.A means that also c2.A changes too. ----- BEGIN example CODE -----------
9
2663
by: Laban | last post by:
Hi, I find myself using static methods more than I probably should, so I am looking for some advice on a better approach. For example, I am writing an app that involves quite a bit of database operations on purchase orders and inventory. I have created a PurchaseOrder class and Inventory class to encapsulate operations like creating POs, finding items, etc. These two classes are used extensively from different parts of the app.
3
5514
by: Mark Dunmill | last post by:
I can't create a Constant/Read-only array field in managed C++ classes - doesn't allow the keyword const pointer to const object on array fields in managed C++ classes. e.g. Want to define a read/only field for an empty array (so all the empty arrays of the given type can use the same object) however because the pointer in the field cannot be marked as constant then it is possible that any external party can alter this pointer to reference...
11
3844
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
11
3054
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
2
1630
by: Random | last post by:
Here's a design question I'm curious to know if anyone here has wrestled with before... I'm writing my data access methods in classes in the App_Code directory. I know that I can easily instantiate each class on a page and run it's functions to get my data from the database. However, I'm wondering if I could pick up performance if I made all the functions shared/static. Then I'm wondering if it would be worth it because of the design...
6
3566
by: Marvin Barley | last post by:
I have a class that throws exceptions in new initializer, and a static array of objects of this type. When something is wrong in initialization, CGI program crashes miserably. Debugging shows uncaught exception. How to catch an exception that happened before main() try { ... } catch (...) { ... } block? Is there a way?
14
6027
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared static inside functions (i.e. local static objects) 5. objects declared at file scope.
4
3365
by: Steffen Bobek | last post by:
Extension methods are made for use with instances. I'd like to "misuse" them as static methods, too. Let me tell you my ambition: I use an extension method to serialize objects somehow like this: MyObject obj = new MyObject(); obj.ToXmlFile("some directory\\some file.xml"); The method "ToXmlFile" is defined elsewhere far apart from MyObject to work for all objects:
0
9621
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
9454
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
10106
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...
1
10039
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
9914
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
8937
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...
1
7461
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3610
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.