What in the HECK is going on??? | | |
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 | | | | re: What in the HECK is going on???
"Randy" <yates@ieee.org> wrote in message
news:1137024713.946890.316120@g14g2000cwa.googlegr oups.com...[color=blue]
> Compiling the following code
>
>
> #include <iostream>
> #include "cenum.h"
> using namespace std;[/color]
[color=blue]
>
> class Test
> {
> CEnum MyCars("CAR", "Mustang, Nova, Pinto, Barracuda");[/color]
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.
[color=blue]
> Test();[/color]
add:
Test(const CEnum& c) : myCars(c)
{
}
[color=blue]
> ~Test();
> };
>
> int main(int argc, char **argv)
> {[/color]
Test(CEnum("CAR", "Mustang, Nova, Pinto, Barracuda"));
[color=blue]
> return 0;
> }[/color]
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 | | | | re: What in the HECK is going on???
Randy wrote:[color=blue]
> Compiling the following code
>
>
> #include <iostream>
> #include "cenum.h"
> using namespace std;
>
> class Test
> {
> CEnum MyCars("CAR", "Mustang, Nova, Pinto, Barracuda");[/color]
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.
[color=blue]
> 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.[/color]
Right, because you can initialize a variable at global (or namespace)
scope.
[color=blue]
> What the heck is the problem here?
>
> Here's cenum.h:
>[/color]
[color=blue]
>
> #ifndef _CENUM_H
> #define _CENUM_H[/color]
[long comments snipped]
[color=blue]
> #include <string>
> #include <vector>
> using namespace std;[/color]
The above line is generally a bad thing to include in a header file.
[remainder of header snipped.]
Best regards,
Tom | | | | re: What in the HECK is going on???
Randy wrote:[color=blue]
> Compiling the following code
>
>
> #include <iostream>
> #include "cenum.h"
> using namespace std;
>
> class Test
> {
> CEnum MyCars("CAR", "Mustang, Nova, Pinto, Barracuda");[/color]
This is a (badly formed) function declaration, not an object initialisation.
Ian | | | | re: What in the HECK is going on???
Ian <ian-news@hotmail.com> writes:
[color=blue]
> Randy wrote:[color=green]
>> Compiling the following code
>> #include <iostream>
>> #include "cenum.h"
>> using namespace std;
>> class Test
>> {
>> CEnum MyCars("CAR", "Mustang, Nova, Pinto, Barracuda");[/color]
>
> This is a (badly formed) function declaration, not an object initialisation.[/color]
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'
%%%% <yates@ieee.org> % *Time*, Electric Light Orchestra http://home.earthlink.net/~yatescr |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,384 network members.
|