CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2003
    Posts
    7

    a structure with a vector as a member

    Hello:
    I have this interesting problem...
    I have to use a c-library that contains a function that is defined as follow:

    int func(char * cpData);

    I have been using a structure accually a nested structure as follows

    struct A
    {
    char cString;
    char cAnotherstring;
    }sA;

    struct B
    {
    int iIndex;
    float fData[MAXDATA];

    }sB;

    struct C
    {
    struct A nA;
    struct B nB[8];

    }sStream;


    I can call this structure with now problems
    using func((char *) &sStream);


    I wanted to use a vector for the float fData

    such as the struct B is as follows:

    struct B
    {
    int iIndex;
    vector< float> fData;
    } sB;


    when I call the function I getting garbaged data.
    I do take care of reserving the data size.
    In fact I can see that the correct data iscorrect, just prior to calling
    this legecy function.

    Is there a way I can convert somehow to c-array which the function understands
    but be able to take advantage of the vector.

    Your speedy reply wiill be vey much appreciated.

    AD

  2. #2
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    Re: a structure with a vector as a member

    I already answered this question in the other thread you had going. Maybe a mod could flip my post over here since the question/answer is not really related to that thread.
    Insert entertaining phrase here

  3. #3
    Join Date
    Dec 2003
    Posts
    7

    Re: a structure with a vector as a member

    As you see I have to use a c library, and I can not do what you are suggesting
    can I, I geuss May be I do not understand what you have said.
    Could you elaborate more.

    Thanx

    AD

  4. #4
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    Re: a structure with a vector as a member

    Well.. You are using a std::vector, so you're obiously on C++. If you serialize your struct using >>, << and std::ostream (std::ostringstream to be specific), you can still plug the result of that operation into a C function by getting a std::string of the stream (str() member-function), and passing that string to the C-function using std::string's c_str() member-function.
    Insert entertaining phrase here

  5. #5
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    Re: a structure with a vector as a member

    Note that you won't get the binary representation of the struct by using std::ostream though. Only a textual representation. If you need the binary representation, you will have to write your own serializer. I did write one myself a while back, and I would have shared, if only I could find it.
    Insert entertaining phrase here

  6. #6
    Join Date
    Dec 2003
    Posts
    7

    Re: a structure with a vector as a member

    That is over my head for now.
    But I will chew on it for a while
    Thank you for yor time

    AD

  7. #7
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    Re: a structure with a vector as a member

    Insert entertaining phrase here

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured