help with a structure

This is a discussion on help with a structure within the C++ Programming forums, part of the General Programming Boards category; Hi all. Been a while since I've posted in here. I've been writing a large application in Java, only to ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    163

    help with a structure

    Hi all. Been a while since I've posted in here. I've been writing a large application in Java, only to find that it's impossible to continue in Java since it tries so hard to not allow you to access individual bytes, but the bossman wanted it in Java. So now I'm porting it over to C++, and I've run into a few difficulties, the first being with a structure.

    Now, I should also explain that there's two distinct pieces of software here. One being an embedded group, and the other being a control station group. I'm writing for the control station group, and the embedded group has given me their code to speed up the switch to c++.

    I have a class called kCom. This structure is defined in the header file:
    Code:
    struct timestamp{
    	    unsigned int year:6;
    	    unsigned int month:4;
            unsigned int day:5;
            unsigned int hour:5;
            unsigned int minute:6;
            unsigned int second:6;
        };
    I've never seen syntax like that before (the year:6, month:4 thing). What's that accomplishing?

    The second problem I'm facing is when I try to actually use the struct in another called startCom:
    Code:
    void startCom::runChoice(int choice)
    {
        kCom bigK;
        bigK.timestamp Cal;
    etc....
    }
    I get this error:
    Code:
    error: invalid use of 'struct kCom::timestamp'
    error: expected ; before "Cal"
    What's causing this? I've been away from c++ for a while, so it's taking me some time to get used to it again.
    Thanks for any help you can give.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The structure is defined with limited bitwidth integers.

    For example, unsigned int year:6, means that it's a 6-bit unsigned integer, so it can hold values between 0..63

    I'm not entirely sure what you want to do with:
    Code:
    void startCom::runChoice(int choice)
    {
        kCom bigK;
        bigK.timestamp Cal;
    ...
    But I think you want "kCom::timestamp Cal;" - whether you ened a kCom bigK as well or not is a different question that I can't answer without seeing more of the code.

    --
    Mats

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    163
    Quote Originally Posted by matsp View Post
    The structure is defined with limited bitwidth integers.

    For example, unsigned int year:6, means that it's a 6-bit unsigned integer, so it can hold values between 0..63

    I'm not entirely sure what you want to do with:
    Code:
    void startCom::runChoice(int choice)
    {
        kCom bigK;
        bigK.timestamp Cal;
    ...
    But I think you want "kCom::timestamp Cal;" - whether you ened a kCom bigK as well or not is a different question that I can't answer without seeing more of the code.

    --
    Mats

    Ah, thanks so much. Guess I'm just showing that I've been working exclusively with Java for several months.

    I would show more of the code but it's still way unorganized and nowhere near a complete level where anything will make sense.


    Basically what's happening is that kCom holds the timestamp structure, and encodes it in a specific format(defined by the embedded team) when needed. There are also mime encode/decode functions, ks64(in house codec) encode/decode, and a packet formatter.

    I'm sure I'll be back on here asking more and more questions, so you'll get to see the code grow over time

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    163
    Sorry for the back to back posting. I've resolved many of the issues that have come up, but this one seems to have me stumped. It's a linker error. If this was 6 months ago I'd know how to fix it, unfortunately I've forgotten a lot of my little tricks. These are the errors:

    Code:
    Project   : Console application
    Compiler  : GNU GCC Compiler (called directly)
    Directory : C:\Documents and Settings\Andrew\My Documents\UKGS\
    --------------------------------------------------------------------------------
    Switching to target: default
    Compiling: kCom.cpp
    Linking console executable: C:\Documents and Settings\Andrew\My Documents\UKGS\UKGS.exe
    .objs\kCom.o:kCom.cpp:(.text+0x814): undefined reference to `mime::ks64_encode(unsigned char*, unsigned char)'
    .objs\kCom.o:kCom.cpp:(.text+0x87e): undefined reference to `mime::mime_encode(unsigned char*, unsigned char*, unsigned char)'
    .objs\kCom.o:kCom.cpp:(.text+0x8e4): undefined reference to `mime::mime_encode(unsigned char*, unsigned char*, unsigned char)'
    .objs\kCom.o:kCom.cpp:(.text+0x941): undefined reference to `mime::ks64_encode(unsigned char*, unsigned char)'
    collect2: ld returned 1 exit status
    Process terminated with status 1 (0 minutes, 0 seconds)
    I'm using Code::Blocks with miniGW, running XP through parallels on a Macbook Pro.

    I can't show the entire sections of the code because it's somewhat classified, but this is an example of where the error is displaying:
    In kCom.cpp
    Code:
    if( newMime.ks64_encode( &enc_data[enc_index], cmd ) != 0x00 )
    mime.cpp (edited for confidentiality)
    Code:
    unsigned char ks64_encode(unsigned char *dest,unsigned char raw )
    {
        if((thing <= thing))
            //do stuff
        else if((thing >= thing) && (thing <= thing))
             //do stuff
        else if((thing >= thing) && (thing <= thing))
             //do stuff
        else if (thing == thing)
             //do stuff
        else if (thing == thing)
             //do stuff
        return thing
    }
    mime.h
    Code:
    #ifndef MIME
    #define MIME
    
    class mime
    {
    
        public:
            mime();
            ~mime();
            unsigned char mime_encode(unsigned char *dest, unsigned char *raw_data, unsigned char num_raw);
            unsigned char mime_decode(unsigned char *dest, unsigned char *num_raw, unsigned char *enc_data);
    
            unsigned char ks64_encode(unsigned char *dest, unsigned char raw);
            unsigned char ks64_decode(unsigned char *dest, unsigned char character);
    
        private:
    
    };
    #endif

    Anything that can get rid of this little error will be great. It's been tormenting me for a while now.
    Last edited by System_159; 08-06-2007 at 03:48 PM. Reason: messed up code tags

  5. #5
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    unsigned char ks64_encode(unsigned char *dest,unsigned char raw )

    looks like a regular function to me, but since in the header it's a member function, I'd suggest this change

    unsigned char mime::ks64_encode(unsigned char *dest,unsigned char raw )

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    163
    Thanks Darryl, that worked. Guess I forgot to add the class stuff in the mime.cpp file.

    Next question. I need to convert an unsigned long to an array of unsigned chars. Does anybody have any quick ways of doing this?


    edit: I think I may have got it actually, found something on av.com right after I posted
    Code:
                 raw_period[3] = (unsigned char)period;
                 raw_period[2] = (unsigned char)(period >> 8);
                 raw_period[1] = (unsigned char)(period >> 16);
                 raw_period[0] = (unsigned char)(period >> 24);
    where raw_period is the unsigned char array, and period is the unsigned long
    Last edited by System_159; 08-07-2007 at 09:39 AM. Reason: whoops, posted before I was done.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you want to be really ugly (and you know both ends are as happy with the current byte order):
    Code:
    union {
       unsigned char c[4];
       unsigned long l;
    };
    
    xx.l = some_number;
    xx.c now contains 4 chars...

    --
    Mats

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 06:46 PM
  2. Replies: 5
    Last Post: 02-14-2006, 09:04 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 05:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21