Find the answer to your Linux question:
Results 1 to 9 of 9
Alrighty. I'm having a few issues with using a string or a struct as a type definition in C++. Firstly, my book is telling me that the declaration Code: struct ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252

    C++ Can't Seem to Use string or a Struct


    Alrighty. I'm having a few issues with using a string or a struct as a type definition in C++.

    Firstly, my book is telling me that the declaration

    Code:
    struct time theTime
    is exactly the same as

    Code:
    time theTime
    However, my compiler gives me a

    Code:
    ex12-2.cpp:13: error: 'time' is used as a type, but is not defined as a type.
    If I don't use the "struct" before the declaration.

    The relevant code is:

    Code:
    struct time
    {
    	unsigned int hours; // Number of hours (24-hour clock)
    	unsigned int mins; // Number of minutes
    };
    
    struct date_time
    {
    	string date;  // In the MMDDYYYY format.  Example: 03231988 is March 23, 1988
    	time theTime;  // The Time.  Declare as theTime = {hours, minutes}
    };

    And that segways nicely into the next question.

    In the above, I declare "string date". However, in my compiler, I receive an error of:

    Code:
    ex12-2.cpp:12: error: 'string' is used as a type, but is not defined as a type.
    Yes, I am including string.


    So, if anyone can explain what I'm doing wrong here, I'd be quite grateful. Thanks in advance!

  2. #2
    Linux Guru AlexK's Avatar
    Join Date
    Feb 2005
    Location
    Earth
    Posts
    3,379
    Quote Originally Posted by Cabhan
    Code:
    struct time theTime
    is exactly the same as

    Code:
    time theTime
    not True. try this
    Code:
    typedef struct time TheTime
    then you can go around writing time TheTime. Having the typedef, tells C/C++ that whenever it encounters time xxxx, it has type struct time.

    Um, C/C++ doesnt have a string data type unlike VB. You will have to use a character array of sorts like:
    Code:
     char date[9]  // the size is 9 for the ending null character
    or
    char *date  // where you can malloc as much space as you need for date
    Personally, I would use the char date[9] option to have the date as shown in your code. Also you should take a look at time.h if you are interested in getting the system date and time etc.

    hope this helps.

  3. #3
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    C++ does have a string type O.o. C forces you to use an array of chars, but C++ has its own string class, accessed by

    Code:
    #include <string>
    I found out the problem: I forgot that string was a part of the std namespace .


    But is there no other way to omit the "struct"? Because my book explicitly says that C++ allows you to do so.

    The book is "Practical C++ Programming" from O'Reilly, btw.

  4. #4
    Linux Guru AlexK's Avatar
    Join Date
    Feb 2005
    Location
    Earth
    Posts
    3,379
    Actually I made a mistake in my previous post, when you type typedef time TheTime, you cant go writing time TheTime, it actually will be more like
    Code:
    TheTime SomeTime;
    since GCC seems to follow strictly to ANSI C, you will have to use the typedef in order to get rid of the errors, the book you are talking about is probably written for Visual Studio which is not as strict. However, there may be other C standards such as ISO which may allow you to get away with it. And I dont know of any ISO compilers in Linux, but I prefer to stick to ANSI standard as it makes the code more readable.

    hope this helps.
    Life is complex, it has a real part and an imaginary part.

  5. #5
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Alrighty. .

    Thanks, though.

  6. #6
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    Just to add a bit to the above.You can write
    Code:
    typedef struct &#123; the fields of your structure &#125; TheTime ;
    instead of
    Code:
    typedef struct time &#123; the fields of your structure &#125; TheTime ;
    In both cases you can define new instances of the structure by writing
    Code:
    TheTime my_time ;
    But in the later case you can also write
    Code:
    struct time my_time ;
    Or at least that's how it works in C , I'm not sure about C++

  7. #7
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    Quote Originally Posted by Cabhan
    Alrighty. .
    Why the long face , you got what you wanted , didn't you ?

  8. #8
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Quote Originally Posted by Santa's little helper
    Quote Originally Posted by Cabhan
    Alrighty. .
    Why the long face , you got what you wanted , didn't you ?
    My book lied to me :P.

  9. #9
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by Cabhan
    My book lied to me :P.
    Books do that.

Posting Permissions

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