What in the HECK is going on???

Discussion in 'C++' started by Randy, Jan 12, 2006.

  1. Randy

    Randy Guest

    Compiling the following code


    #include <iostream>
    #include "cenum.h"
    using namespace std;

    class Test
    {
    CEnum MyCars("CAR", "Mustang, Nova, Pinto, Barracuda");
    Test();
    ~Test();
    };

    int main(int argc, char **argv)
    {
    return 0;
    }



    I get


    /home/yates/modetest/host/app/modetest/cenumbug.cpp:7: error: expected
    identifier before string constant
    /home/yates/modetest/host/app/modetest/cenumbug.cpp:7: error: expected
    `,' or `...' before string constant
    /home/yates/modetest/host/app/modetest/cenumbug.cpp:7: error: ISO C++
    forbids declaration of `parameter' with no type




    If, however, I pull the CEnum declaration out and put it in global
    scope, it compiles fine.
    What the heck is the problem here?

    Here's cenum.h:




    #ifndef _CENUM_H
    #define _CENUM_H
    /*******************************************************************************
    Module: Enumeration Class Template (CEnum)
    Author: Randy Yates
    Creation Date: 10-Jan-2006
    Description:

    CEnum provides an enumeration class. Use CEnum as follows:

    CEnum MyCars("CAR", "Mustang, Nova, Pinto, Barracuda");

    The mechanism provides the following functionality:

    1. Each enumeration identifier (e.g., "Mustang") and enumeration
    typename (e.g., "CAR") is string-ized.

    2. Each identifier is associated with a signed, 16-bit integer,
    just as C's standard enumeration mechanism. For example, CEnum
    can perform the equivalent of

    typedef {First=-2, Second, Third=300, Fourth} MyEnumType;

    where the initializers are optional.

    3. CEnums overload the following operators:

    MyCars++;
    MyCars--;
    MyCars = "Nova";

    Iterators are incremented and decremented modulo ::Count().

    4. Provides the following public member functions:

    ::Value() retrieves integer value of current
    enumeration identifier.
    ::Value(uint16_t n) retrieves integer value of nth
    enumeration identifier.
    ::String() retrieves string of current enumeration
    identifier.
    ::String(uint16_t n) retrieves string of nth enumeration
    identifier.
    ::Type() retrieves string of enumeration typename.
    ::Begin() retrieves beginning enumeration
    identifier index (e.g., for use
    in ::Value(n)). Indices are 0-based.
    ::Current() retrieves current enumeration identifier
    index.
    ::End() retrieve ending enumeration identifier
    index.
    ::Count() retrieves number of enumeration
    identifiers.
    ::Set(uint16_t n) sets enumeration index to n.
    ::Set(string str) sets enumeration index to index
    enumeration identifier string
    corresponding to str. E.g.,
    MyCars.Set("Mustang");

    5. Provides inserters that will operate as in the following
    example:

    cout << MyCars;

    yields

    "CAR=Mustang"

    6. Provides extractors that will operate as in the following
    example:

    cin >> MyCars;

    when cin is "CAR=Mustang" will set the current enumeration
    identier to "Mustang".
    Note that this is equivalent to ::Set("Mustang").

    7. When constructed, the default enumeration identifier index
    will be set to 0.

    Comment:

    In order to "typedef" a specific enumeration type and use it in
    multiple
    instances (yes, this is kludgie, but I couldn't see a better way),
    do this:

    #define CAR_INSTANCE(a) CEnum a("CAR", "Mustange, Nova, Pinto,
    Barracuda")

    and then

    CAR_INSTANCE MyCars;

    *******************************************************************************/
    #include <string>
    #include <vector>
    using namespace std;
    #include <stdint.h>

    class CEnum
    {
    uint16_t index;
    uint16_t count;
    vector<string> strEnumIDs;
    vector<int> strEnumValues;
    string strEnumType;

    public :

    enum {TOKEN_MAX_CHARS=256};
    CEnum();
    CEnum(string strEnumType, string strIdentifiers);
    CEnum(string strEnumType, string strIdentifiers, string
    strInitialIdentifier);
    CEnum(string strEnumType, string strIdentifiers, uint16_t
    nInitialIdentifier);
    void Construct(string strEnumType, string strIdentifiers);
    uint16_t Value();
    uint16_t Value(uint16_t n);
    string String();
    string String(uint16_t n);
    string Type();
    uint16_t Begin();
    uint16_t Current();
    uint16_t End();
    uint16_t Count();
    bool Set(uint16_t n);
    bool Set(string str);
    string Dump();
    CEnum operator++();
    CEnum operator++(int notused);
    CEnum operator--();
    CEnum operator--(int notused);
    virtual ~CEnum();

    friend ostream& operator<<(ostream& os, CEnum& ce);
    friend istream& operator>>(istream& is, CEnum& ce);
    };

    #endif


    Please help!

    --Randy
    Randy, Jan 12, 2006
    #1
    1. Advertising

  2. Randy

    Mike Wahler Guest

    "Randy" <> wrote in message
    news:...
    > Compiling the following code
    >
    >
    > #include <iostream>
    > #include "cenum.h"
    > using namespace std;



    >
    > class Test
    > {
    > CEnum MyCars("CAR", "Mustang, Nova, Pinto, Barracuda");


    change to:

    CEnum MyCars;

    You're trying to create an object in a declaration. A class
    definition only declares its members, it doesn't (can't)
    create objects.

    > Test();


    add:

    Test(const CEnum& c) : myCars(c)
    {
    }

    > ~Test();
    > };
    >
    > int main(int argc, char **argv)
    > {


    Test(CEnum("CAR", "Mustang, Nova, Pinto, Barracuda"));

    > return 0;
    > }


    The reason it 'worked' with the CEnum declaration (which was
    also a definition) at global scope is because it's OK to
    create objects there. Inside a class definition, it's not.

    There could be more wrong with your code, I didn't look any
    further.

    -Mike
    Mike Wahler, Jan 12, 2006
    #2
    1. Advertising

  3. Randy wrote:
    > Compiling the following code
    >
    >
    > #include <iostream>
    > #include "cenum.h"
    > using namespace std;
    >
    > class Test
    > {
    > CEnum MyCars("CAR", "Mustang, Nova, Pinto, Barracuda");


    The above line is an error. You can't initialize MyCars in the Test
    class declaration. If MyCars was meant to be a static member of the
    class (in which case you omitted the "static" keyword), you must
    initialize MyCars outside of the declaration. If MyCars is an ordinary
    member, then you must initialize it in the constructor. You do
    neither.

    > Test();
    > ~Test();
    > };
    >
    > int main(int argc, char **argv)
    > {
    > return 0;
    > }
    >
    >
    > I get
    >
    >
    > /home/yates/modetest/host/app/modetest/cenumbug.cpp:7: error: expected
    > identifier before string constant
    > /home/yates/modetest/host/app/modetest/cenumbug.cpp:7: error: expected
    > `,' or `...' before string constant
    > /home/yates/modetest/host/app/modetest/cenumbug.cpp:7: error: ISO C++
    > forbids declaration of `parameter' with no type
    >
    >
    >
    >
    > If, however, I pull the CEnum declaration out and put it in global
    > scope, it compiles fine.


    Right, because you can initialize a variable at global (or namespace)
    scope.

    > What the heck is the problem here?
    >
    > Here's cenum.h:
    >


    >
    > #ifndef _CENUM_H
    > #define _CENUM_H


    [long comments snipped]

    > #include <string>
    > #include <vector>
    > using namespace std;


    The above line is generally a bad thing to include in a header file.

    [remainder of header snipped.]

    Best regards,

    Tom
    Thomas Tutone, Jan 12, 2006
    #3
  4. Randy

    Ian Guest

    Randy wrote:
    > Compiling the following code
    >
    >
    > #include <iostream>
    > #include "cenum.h"
    > using namespace std;
    >
    > class Test
    > {
    > CEnum MyCars("CAR", "Mustang, Nova, Pinto, Barracuda");


    This is a (badly formed) function declaration, not an object initialisation.

    Ian
    Ian, Jan 12, 2006
    #4
  5. Randy

    Randy Yates Guest

    Ian <> writes:

    > Randy wrote:
    >> Compiling the following code
    >> #include <iostream>
    >> #include "cenum.h"
    >> using namespace std;
    >> class Test
    >> {
    >> CEnum MyCars("CAR", "Mustang, Nova, Pinto, Barracuda");

    >
    > This is a (badly formed) function declaration, not an object initialisation.


    Ahh, thanks Ian. That explains the error message, which is what was
    really throwing me off. Point taken on the namespace comment as well.

    Thanks to everyone else as well. I appreciate the help.
    --
    % Randy Yates % "Ticket to the moon, flight leaves here today
    %% Fuquay-Varina, NC % from Satellite 2"
    %%% 919-577-9882 % 'Ticket To The Moon'
    %%%% <> % *Time*, Electric Light Orchestra
    http://home.earthlink.net/~yatescr
    Randy Yates, Jan 12, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin Nicholson

    what the heck is "ASP.NET Web Matrix" ?

    Martin Nicholson, Oct 27, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    412
    Steve C. Orr [MVP, MCSD]
    Dec 15, 2003
  2. Paul W

    what the heck?!?!?!?

    Paul W, Apr 6, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    433
    Paul W
    Apr 6, 2005
  3. =?Utf-8?B?V2lsbGlhbSBTdWxsaXZhbg==?=

    Fill a cell with a control -- how the heck?

    =?Utf-8?B?V2lsbGlhbSBTdWxsaXZhbg==?=, Aug 18, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    455
    Eliyahu Goldin
    Aug 18, 2005
  4. Replies:
    1
    Views:
    280
    Peter Otten
    Feb 27, 2007
  5. Rotwang
    Replies:
    4
    Views:
    266
    Rotwang
    Jan 28, 2010
Loading...

Share This Page