Topic
IC4NOTICE: developerWorks Community will be offline May 29-30, 2015 while we upgrade to the latest version of IBM Connections. For more information, read our upgrade FAQ.
9 replies Latest Post - ‏2012-10-21T09:46:46Z by Mathias Mamsch
MichaelGeorg
MichaelGeorg
53 Posts
ACCEPTED ANSWER

Pinned topic Cause of: Incorrect context for function call

‏2012-10-11T12:52:20Z |

Hello.

Does someone her know the cause of the message "Incorrect context for function call" ?

In my case I'm getting this error message for line 15 of the following code:

const string fullNameSpecIndex = "\Project\Spec_Index"
 
string buildStringArrayOfAttributeType(AttrType at)[] {
    int i
 
        string result[at.size]
        for i in 0:at.size - 1 do {
                result[i] = at.strings[i]
        }
        return result
}
 
Module modSpecIndex = read(fullNameSpecIndex, false) 
AttrType typeSupplier = find(modSpecIndex, "t_Supplier")
string arrSuppliers[] = buildStringArrayOfAttributeType(typeSupplier)

 


Would be great if I could get a hint about the problem here and about the meaning of the error message.
- Michael

 

Updated on 2014-01-06T15:23:55Z at 2014-01-06T15:23:55Z by iron-man
  • adevicq
    adevicq
    154 Posts
    ACCEPTED ANSWER

    Re: Cause of: Incorrect context for function call

    ‏2012-10-11T13:09:55Z  in response to MichaelGeorg

    Hi,
    I'm not sure that you can assign an array this way.
    You should try to pass a reference of your array to your initialization function:
     

    void buildStringArrayOfAttributeType(AttrType at, string &szArray[])
    

     


    Alain

     

    Updated on 2014-01-06T15:24:07Z at 2014-01-06T15:24:07Z by iron-man
    • adevicq
      adevicq
      154 Posts
      ACCEPTED ANSWER

      Re: Cause of: Incorrect context for function call

      ‏2012-10-11T13:30:15Z  in response to adevicq

      Hi again,

      I tried this:
       

      void x(string  &s[]) {
      int i
          for (i=0; i < sizeof(s); ++i)
                      s[i] = "elem " i ""
      }
       
       
      string w[4]
      x(w)
       
      for (i = 0; i < sizeof(w); ++i) {
          print "elem " i "\n"
      }
      

       


      and it works...
      It seems you cannot ommit to declare the array size...
      Alain

       

      Updated on 2014-01-06T15:24:21Z at 2014-01-06T15:24:21Z by iron-man
      • adevicq
        adevicq
        154 Posts
        ACCEPTED ANSWER

        Re: Cause of: Incorrect context for function call

        ‏2012-10-11T13:34:26Z  in response to adevicq
        correction...
        you have to indicate the array size in the function daclaration.
        • MichaelGeorg
          MichaelGeorg
          53 Posts
          ACCEPTED ANSWER

          Re: Cause of: Incorrect context for function call

          ‏2012-10-11T13:55:56Z  in response to adevicq

          @adevicq: Thanks for your help.

          Honestly I'm even more confused now. I'm sure to have tested a function like

          string x()[]
          

          successfully before I did build it into my code - but now I can't reproduce that :-(

          So I need to know the size before calling the function?
          As that would not be possible in my case I will then go with a Skip instead and then use other functions to get its size and convert it to an array.

          Updated on 2014-01-06T15:24:33Z at 2014-01-06T15:24:33Z by iron-man
          • adevicq
            adevicq
            154 Posts
            ACCEPTED ANSWER

            Re: Cause of: Incorrect context for function call

            ‏2012-10-12T12:55:42Z  in response to MichaelGeorg
            I would use a skip list instead...
            Alain
  • llandale
    llandale
    2943 Posts
    ACCEPTED ANSWER

    Re: Cause of: Incorrect context for function call

    ‏2012-10-12T16:49:16Z  in response to MichaelGeorg
    I don't think you can use arrays like this. The array is allocated inside the function (on the stack) and de-allocated when the function terminates.

    For this particular problem, I do this:
    • int NumberEnums = CountEnumeratedValues(at)
    • string EnumValues(NumberEnums)
    • PopulateEnums(at, EnumValues)

    -Louie
  • Mathias Mamsch
    Mathias Mamsch
    1946 Posts
    ACCEPTED ANSWER

    Re: Cause of: Incorrect context for function call

    ‏2012-10-13T12:09:00Z  in response to MichaelGeorg
    We have a lot of lengthy discussion on the forum, about why returning arrays does not work and what you can do instead. Maybe it helps:

    https://www.ibm.com/developerworks/forums/thread.jspa?messageID=14301349&#14301349
    https://www.ibm.com/developerworks/forums/thread.jspa?messageID=14472558#14472558
    https://www.ibm.com/developerworks/forums/thread.jspa?messageID=14472624&#14472624
    https://www.ibm.com/developerworks/forums/thread.jspa?messageID=14777803&#14777803

    Maybe that helps, regards, Mathias

    Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS
  • MichaelGeorg
    MichaelGeorg
    53 Posts
    ACCEPTED ANSWER

    Re: Cause of: Incorrect context for function call

    ‏2012-10-16T13:18:32Z  in response to MichaelGeorg
    Thanks for all your broader feedback - except that I still don't know the real meaning of the error message the problem is solved for me now.

    @Louie: First counting the enum entries is just the way I've gone now. (Even so I still don't really like that way.)
    @Mathias: Thanks for all the additional links. My conclusion form them is you can't or at least shouldn't use arrays as return values, like Louie and Alain pointed out above. :-)
    • Mathias Mamsch
      Mathias Mamsch
      1946 Posts
      ACCEPTED ANSWER

      Re: Cause of: Incorrect context for function call

      ‏2012-10-21T09:46:46Z  in response to MichaelGeorg

      Well the error message "invalid context for function call" tells you, that you did the function call alright, but you put it in an assignment that DXL does not know how to evaluate. The only way of assigning arrays during declaration in DXL is:
       

      int ar[] = {1,2,3,4,5}
      

       

      Now the parser finds an array declaration that matches the return value of your function, but it does not support assignment. A similar thing happens, when you try to do this:

       

       

      int func() { return 3 }
       
      int &ref = func()
      



      Func returns an integer, and the ref variable has an integer type, BUT for reference variables you can only assign another variable. So it tells you used func in the wrong "context". Maybe that helps. Regards, Mathias



       

       

       


      Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

       

       

      Updated on 2014-01-06T15:25:15Z at 2014-01-06T15:25:15Z by iron-man