473,320 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

compilation error

Slightly surprised that the following didn't compile

#inclue <algorithm>
enum { C = 10 };
int main()
{
char a[C];
std::fill_n(a, C, '\0');
}

Error message points to implementation of std::fill_n and complains that

unary '--' : '' does not define this operator or a conversion to a type
acceptable to the predefined operator

Essentially I think its complaining that it can't modify the value of the
enum C.

Is this correct? I expected the template to be instantiated with an int
which could be modified using operator--.

Replacing enum { C = 10 }; with const int C = 10; does compile.

John
Jul 19 '05 #1
12 6052


On Tue, 15 Jul 2003, John Harrison wrote:
Slightly surprised that the following didn't compile

#inclue <algorithm>
enum { C = 10 };
int main()
{
char a[C];
std::fill_n(a, C, '\0');
}

Error message points to implementation of std::fill_n and complains that

unary '--' : '' does not define this operator or a conversion to a type
acceptable to the predefined operator

Essentially I think its complaining that it can't modify the value of the
enum C.

Is this correct? I expected the template to be instantiated with an int
which could be modified using operator--.

Replacing enum { C = 10 }; with const int C = 10; does compile.

John

Your code, as above, compiles, as is, with gcc 3.2.2. Did you use fill()
instead of fill_n()?

Jul 19 '05 #2

"Jesse Nowells" <jn******@transbay.net> wrote in message
news:Pine.BSF.4.31.0307150053130.80673-100000@localhost...


On Tue, 15 Jul 2003, John Harrison wrote:
Slightly surprised that the following didn't compile

#inclue <algorithm>
enum { C = 10 };
int main()
{
char a[C];
std::fill_n(a, C, '\0');
}

Error message points to implementation of std::fill_n and complains that

unary '--' : '' does not define this operator or a conversion to a type
acceptable to the predefined operator

Essentially I think its complaining that it can't modify the value of the enum C.

Is this correct? I expected the template to be instantiated with an int
which could be modified using operator--.

Replacing enum { C = 10 }; with const int C = 10; does compile.

John

Your code, as above, compiles, as is, with gcc 3.2.2. Did you use fill()
instead of fill_n()?


No I used code exactly as above, cut and paste from my compiler, except for
#include <algorithm> which I managed to mistype.

Compiler is VC++ .NET

john
Jul 19 '05 #3

"Alf P. Steinbach" <al***@start.no> wrote in message
news:3f****************@News.CIS.DFN.DE...
On Tue, 15 Jul 2003 08:03:01 +0100, "John Harrison" <jo*************@hotmail.com> wrote:
Slightly surprised that the following didn't compile

#inclue <algorithm>
Hah, there's your culprit right there. ;-)


The rest of the code was cut and paste, I swear!

enum { C = 10 };
int main()
{
char a[C];
std::fill_n(a, C, '\0');
}

Error message points to implementation of std::fill_n and complains that

unary '--' : '' does not define this operator or a conversion to a type
acceptable to the predefined operator

enum E{ C = 10 };

E operator--( E x ){ return static_cast<E>( x-1 ); }
Essentially I think its complaining that it can't modify the value of the
enum C.


Nope. It complains that enum type doesn't have a decrement operator.


So presumably this would also compile (don't have my compiler handy to
check)

enum { C = 10 };
int main()
{
char a[C];
std::fill_n(a, (int)C, '\0');
}

john
Jul 19 '05 #4
On Tue, 15 Jul 2003 09:34:34 +0100, "John Harrison" <jo*************@hotmail.com> wrote:
So presumably this would also compile (don't have my compiler handy to
check)

enum { C = 10 };
int main()
{
char a[C];
std::fill_n(a, (int)C, '\0');
}


It does.

Jul 19 '05 #5
What happens if the give the enum type a name? This made it fail to compile
with BCB4.

Fraser.
Jul 19 '05 #6
John Harrison wrote:

Essentially I think its complaining that it can't modify the value of the
enum C.

Is this correct? I expected the template to be instantiated with an int
which could be modified using operator--.


There is no rule excluding enum types from being the first class
citizens when it comes to instantiation of templates.

There is another issue with calling -- on enum types. According to the
C++ standard (5.3.2) one can call the (built-in) prefix operator ++ or
-- on a operand being an lvalue of an arithmetic or pointer to
completely-defined object type. No enum is mentioned in this context
however it is when i.e., the unary +/- operators are discussed.

Nevertheless many compilers will accept such an operation mainly because
of their own backward compatibility so it may be really hard to spot.

Regards,
Janusz

Jul 19 '05 #7
On Tue, 15 Jul 2003 09:33:50 -0400, "Ron Natalie" <ro*@sensor.com> wrote:

"Alf P. Steinbach" <al***@start.no> wrote in message news:3f****************@News.CIS.DFN.DE...

E operator--( E x ){ return static_cast<E>( x-1 ); }


-x is not x-1

E is not necessarily able to hold the value -10.

Nope. It complains that enum type doesn't have a decrement operator.


No it complains about UNARY -.


Hello? Anyone home today, Ron? Must be the heat.

Jul 19 '05 #8
John Harrison wrote:

That's very strange because since enums are often used for ordinal types you
would think that if any arithmetic operators were to be defined then it
would be ++ and --. Just an oversight I guess.


Nope, deliberate. We didn't want to require compiler writers to generate
increment and decrement code for things like this:

enum flags { 0x01, 0x02, 0x04, 0x08, 0x10 };

--

"To delight in war is a merit in the soldier,
a dangerous quality in the captain, and a
positive crime in the statesman."
George Santayana

"Bring them on."
George W. Bush
Jul 19 '05 #9
John Harrison wrote:

"Pete Becker" <pe********@acm.org> wrote in message
news:3F***************@acm.org...
John Harrison wrote:

That's very strange because since enums are often used for ordinal types you would think that if any arithmetic operators were to be defined then it
would be ++ and --. Just an oversight I guess.


Nope, deliberate. We didn't want to require compiler writers to generate
increment and decrement code for things like this:

enum flags { 0x01, 0x02, 0x04, 0x08, 0x10 };


OK I can buy that but then why define unary minus?


--, ++, +=, etc. all must create values of the same type as their
operand. Unary minus, as well as the rest of the arithmetic operators,
create integral values. Read about the "usual arithmetic conversions."

--

"To delight in war is a merit in the soldier,
a dangerous quality in the captain, and a
positive crime in the statesman."
George Santayana

"Bring them on."
George W. Bush
Jul 19 '05 #10

"Pete Becker" <pe********@acm.org> wrote in message
news:3F***************@acm.org...
John Harrison wrote:

"Pete Becker" <pe********@acm.org> wrote in message
news:3F***************@acm.org...
John Harrison wrote:
>
> That's very strange because since enums are often used for ordinal types
you
> would think that if any arithmetic operators were to be defined then

it > would be ++ and --. Just an oversight I guess.
>

Nope, deliberate. We didn't want to require compiler writers to generate increment and decrement code for things like this:

enum flags { 0x01, 0x02, 0x04, 0x08, 0x10 };


OK I can buy that but then why define unary minus?


--, ++, +=, etc. all must create values of the same type as their
operand. Unary minus, as well as the rest of the arithmetic operators,
create integral values. Read about the "usual arithmetic conversions."


OK makes sense, in a twisted kind of way.

john
Jul 19 '05 #11

"Pete Becker" <pe********@acm.org> wrote in message
news:3F**************@acm.org...
John Harrison wrote:

"Pete Becker" <pe********@acm.org> wrote in message
news:3F***************@acm.org...
John Harrison wrote:
>
> "Pete Becker" <pe********@acm.org> wrote in message
> news:3F***************@acm.org...
> > John Harrison wrote:
> > >
> > > That's very strange because since enums are often used for
ordinal types
> you
> > > would think that if any arithmetic operators were to be defined
then it
> > > would be ++ and --. Just an oversight I guess.
> > >
> >
> > Nope, deliberate. We didn't want to require compiler writers to

generate
> > increment and decrement code for things like this:
> >
> > enum flags { 0x01, 0x02, 0x04, 0x08, 0x10 };
> >
>
> OK I can buy that but then why define unary minus?
>

--, ++, +=, etc. all must create values of the same type as their
operand. Unary minus, as well as the rest of the arithmetic operators,
create integral values. Read about the "usual arithmetic conversions."


OK makes sense, in a twisted kind of way.


Why do you call it twisted?


Your explanation makes perfect sense, and the current situation is not an
oversight at all (as I impudentally suggested) but a result of careful
consideration by the standard committee.

Nevertheless, from my point of view, it's still the case that the two
operators that it would make most sense for the standard to define behaviour
for (operator++ and operator--) have no behaviour defined. While operators
which are pretty meaningless for enum do have behaviour defined.

john
Jul 19 '05 #12
John Harrison wrote in news:bf************@ID-196037.news.uni-berlin.de:
Your explanation makes perfect sense, and the current situation is not
an oversight at all (as I impudentally suggested) but a result of
careful consideration by the standard committee.

Nevertheless, from my point of view, it's still the case that the two
operators that it would make most sense for the standard to define
behaviour for (operator++ and operator--) have no behaviour defined.
While operators which are pretty meaningless for enum do have
behaviour defined.


If the behaviour was defined, like it is for int say, you couldn't
do this:

#include <iostream>
#include <ostream>

enum myEnum
{
A, B, C
};

myEnum operator+(myEnum a, myEnum b)
{
int i = (int(a) + int(b)) % int(C + 1);
return myEnum(i);
}

myEnum &operator++(myEnum &a)
{
int i = (int(a) + 1) % int(C + 1);
a = myEnum(i);
return a;
}

myEnum operator++(myEnum &a, int)
{
int i = (int(a) + 1) % int(C + 1);
myEnum b = a;
a = myEnum(i);
return b;
}
int main()
{
myEnum a, b, e;
a = B;
b = C;
e = a + b;

std::cout << int(e) << std::endl;

std::cout << int(++e) << std::endl;

std::cout << int(e++) << std::endl;

std::cout << int(e) << std::endl;

}
Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Joachim | last post by:
I made some project changes (which seems it doesn't help if I undo) which have created compilation error: " Server Error in '/PCSWebApp1' Application....
2
by: James Zhuo | last post by:
Hi all I've been getting the following compilation error. I should explain the background of the project that i am taking over. This is a project that has been developed by someone else a while...
0
by: James Zhuo | last post by:
hi all I changed the name of the class LoginPage to a different name "LoginPageOne" But the same error gets generated with the Wiliam.Request.LoginPageOne. That pretty much leaves me clueless...
0
by: Jie | last post by:
Does anyone know why I keep having the following error. I have to rebuild the solution every time I run it in design mode. thanks jie Server Error in '/ReapPortal' Application....
0
by: John | last post by:
Hi, I've just rolled out a new version of an ASP.NET application. It runs fine on development and training servers, but on production get the following error when trying to access a page: ...
2
by: VB Programmer | last post by:
I created a ASP.NET app locally and it works great. Uploaded it to my server and I get this error when trying to view a page. Any ideas? Server Error in '/earlyalert3' Application....
2
by: Kevin R. | last post by:
I have been ignoring this problem for a few weeks now, but it's becoming a bit annoying not to mention unproductive. Here it goes: I compile my project with no errors. Then after I debug/run it,...
6
by: Plat | last post by:
I've Googled this for a while, to no avail. Hopefully someone can help me. Maybe I'm using the wrong terminology. Here's the scoop! Let's say I've got a simple *.ASPX page that has a syntax...
0
by: Stimp | last post by:
I've created an aspx page called HistoryManage.aspx. The page works fine on my local machine but when I load it off the web I get the following strange error... Compilation Error...
1
by: BSand0764 | last post by:
I'm getting an error that I can't seem to resolve. When I compile the Functor related logic in a test program, the files compile and execute properly (see Listing #1). However, when I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.