Garima Singh
Garima Singh

Reputation: 1490

How to properly assign values to an Array of struct inside another struct:

I am trying to declare an array inside a structure; however, I am having trouble calling it properly. Any help would be appreciated.

I tried it in two different ways: first gives compilation error and second gives linking error. I did extensive search to fix the compilation error myself; but, I could not figure out. Hence, I need some help

I defined the structure as:

struct stPulse_Cal{
    static sSample asArbSqShape[4000];
    double dNormalizedAmplitudeIntegral;
};

Approach 1: Here sSample is another structure defined in third party library and this is how I am using it:

int i;
static sSample asArbSqShape1[4000];
double placeholder[4000];
double dNormalizedAmplitudeIntegral1=0.0;
stPulse_Cal oPulse_Cal1;

// Define the pulse shape (just a trapezoid)
for (i=0; i < 4000; i++)
{
    asArbSqShape1[i].flAbs  = 0.01*i;
    asArbSqShape1[i].flPha  = 0.0;
    placeholder[i] = 0.01*i;
    dNormalizedAmplitudeIntegral1 += placeholder[i];
}

oPulse_Cal1.asArbSqShape            = asArbSqShape1; // Line 130
oPulse_Cal1.dNormalizedAmplitudeIntegral    = dNormalizedAmplitudeIntegral1;

I get the following error: (line no 130 marked in previous code snippet)

SpecialPulses.cpp(130) : error C2106: '=' : left operand must be l-value

Approach 2: When I tried to do something like this for the call part:

int i;
static sSample asArbSqShape1[4000];
double placeholder[4000];
double dNormalizedAmplitudeIntegral1=0.0;
stPulse_Cal oPulse_Cal1;
// Define the pulse shape (just a trapezoid)
for (i=0; i < 4000; i++)
{
    oPulse_Cal1.asArbSqShape[i].flAbs  = 0.01*i;
    oPulse_Cal1.asArbSqShape[i].flPha  = 0.0;
    placeholder[i] = 0.01*i;
    dNormalizedAmplitudeIntegral1 += placeholder[i];
}
oPulse_Cal1.dNormalizedAmplitudeIntegral    = dNormalizedAmplitudeIntegral1;

I get the linking error:

CESTiPAT_OffsetSeriesd_CEST.obj : error LNK2001: unresolved external symbol "public: static struct sSampleTag * stPulse_Cal::asArbSqShape" (?asArbSqShape@stPulse_Cal@@2PAUsSampleTag@@A)
CESTiPAT_OffsetSeriesd_SpecialPulses.obj : error LNK2001: unresolved external symbol "public: static struct sSampleTag * stPulse_Cal::asArbSqShape" (?asArbSqShape@stPulse_Cal@@2PAUsSampleTag@@A)
\n4\x86\prod/bin/CESTiPAT_OffsetSeriesd.dll : fatal error LNK1120: 1 unresolved externals
make: *** [\n4\x86\prod/bin/CESTiPAT_OffsetSeriesd.dll] Error 96

Upvotes: 0

Views: 70

Answers (1)

MichaelCMS
MichaelCMS

Reputation: 4763

In your first approach you try to assign static array to static array, which won't work.

If you had dynamic array you would be able to assign pointers, however this won't give you the result you want, since you most likely want to copy the information,not point to the same memory address.

You can either :

  • copy element by element everything in the array
  • use memcpy for the arrays (faster)

Here's a code snippet for memcpy:

memcpy(oPulse_Cal1.asArbSqShape,asArbSqShape1,4000*sizeof(sSample));

I don't know how sSample is defined as a structure, but if it consists of base types and no pointers then memcpy should give you the result you desire.

For your second problem, I can't tell for sure, but it's most likely that you declared sSampleTag* as static in your structure, and you forgot to declare it in your .cpp as well so an object will contain it.

EDIT !

After a 2nd look at your code, your linkage error is because you declare the asArbSqShape inside your stPulse_Cal as being static ! This means, that there is only one sSample stPulse_Cal::asArbSqShape[4000] program wide... however you only declared it in the structure definition .

There are two ways of approaching this :

  • remove the "static" keyword from your structure.
  • add the bellow code snippet to a cpp file

Code Snippet :

sSample stPulse_Cal::asArbSqShape[4000]

I would go with removing the "static" keyword.

Upvotes: 2

Related Questions