Thread: Declaring enumerated data type

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    2

    Declaring enumerated data type

    I'm asked to create a SortType enumerated data type that stores two values, BY_TITLE and BY_YEAR. I'm then trying to implement a void addBookToList(SortType s, ListType *list, BookType *b) function that finds the new book’s correct insertion point in the list, based on the sort type indicated in the s parameter.

    If the sort type is BY_TITLE, the book must be inserted in alphabetical order by title, and if the sort type is BY_YEAR, the book must be inserted in ascending order by year.

    When I try to create the addBookToList function I keep getting the error:

    expected declaration specifiers or ‘...’ before ‘SortType’ void addBookToList(SortType s, ListType *list, BookType *b)

    I'm not sure if I'm declaring my SortType data type correctly and would appreciate some help.

    defs.h:
    Code:
    enum sort{
        BY_TITLE,
        BY_YEAR
    } SortType;
    
    book.c
    Code:
    void addBookToList(SortType s, ListType *list, BookType *b){
        if(s == BY_TITLE){
        }
        if(s == BY_YEAR){
        }
    }
    
    I tried just changing the data structure in defs.h to:
    Code:
    enum SortType{
        BY_TITLE,
        BY_YEAR
    };
    and the error changed to 'unknown type name 'SortType'; did you mean 'ListType'? void addBookToList(SortType s, ListType *list, BookType *b)
    Last edited by marystew; 11-08-2020 at 11:11 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your enum type is named enum sort. While declaring the enum, you also declared a variable of the enum type named SortType. What you probably wanted to do was to use a typedef so that SortType would be an alias for enum sort:
    Code:
    typedef enum sort{
        BY_TITLE,
        BY_YEAR
    } SortType;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with enumerated data types.
    By And3rs in forum C++ Programming
    Replies: 4
    Last Post: 10-05-2008, 10:06 AM
  2. declaring data type
    By sarathius in forum C Programming
    Replies: 4
    Last Post: 04-17-2008, 04:54 AM
  3. 2-dimesional Array w/ enumerated type
    By partnole in forum C++ Programming
    Replies: 1
    Last Post: 06-02-2007, 01:22 PM
  4. enumerated type and lvalue missing
    By baniakjr in forum C++ Programming
    Replies: 6
    Last Post: 11-22-2006, 07:06 AM
  5. Using enumerated data types
    By SXO in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2001, 06:26 PM

Tags for this Thread