What in the HECK is going on???

R

Randy

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
 
M

Mike Wahler

Randy said:
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.

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
 
T

Thomas Tutone

Randy said:
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
 
I

Ian

Randy said:
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
 
R

Randy Yates

Ian said:
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'
%%%% <[email protected]> % *Time*, Electric Light Orchestra
http://home.earthlink.net/~yatescr
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top