0

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
1
  • You cant assign a value to an Array even if the value is another Array. Try copying it's values instead. Commented Aug 12, 2014 at 10:18

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

You should use std::copy and not memcpy because sSample may require complex copying behaviour.
@MichaelCMS Thanks for helping me out. I tried to use your code snippet regarding memcpy; however, I could not figure out where should I put command related to "sSampleTage". I am really puzzled about "sSampleTag". I have defined only member for struct "sSample". I tried to search for "sSampleTag" in solution, there is no mention anywhere. Then, I do not know what linker is complaining about. Do you think that sSampleTag may be getting defined in third party library.
Using either "memcpy" or std::copy did not help. Both returns the same linking error about sSampleTag. Since I have only used struct sSample, I guess that this is the complex copying behavior @Neil Kirk talked about.
Could you add the code snippet where the sSample structure is defined ?.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.