Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Visual C++ thinks a constant is a virtual function. Help!

7 views
Skip to first unread message

Hurley

unread,
Oct 7, 1999, 3:00:00 AM10/7/99
to
I get a compiler error in Microsoft Visual C++ 5.0 when I try to use the line

static const int size = 10; // or similar expression

This code compiles just fine with other compilers. I documented the exact
error code in the small bag class below. For some reason the compiler thinks
I'm using a virtual function instead of a constant integer. I'm the only one
in my class using VC++ 5.0, so my instructor could not help me. I really
don't want to buy a new compiler. There must be a way to fix this. (Note:
this is code the instructor handed out in class, so changing the code is not
an option).

Can anyone shed some light on this error?

Thanks,
-Hurley
( remove "nospam" from my e-mail to reply )


// Program - shortbag.h
class CShortBag
{
public:
CShortBag();
int size();
int occurances( short element );
void insert( short element );
void remove( short element );

private:
static const int m_capacity = 32; // ERROR 1
short m_data[m_capacity];
short m_used;
};

/*
ERROR 1 : the line cause a comiler error C2258:

C2258: illegal pur syntax, must be '= 0'


A pure virtual function was declared with
incorrect syntax.

The following is an example of this error:

class A
{
public:
void virtual func1() = 1; // error, not = 0
void virtual func2() = 0; // OK
};
*/
// end of shortbag.h


// Program shortbag.cpp
#include "shortbag.h"

CShortBag::CShortBag()
{
m_used = 0;
}


int CShortBag::size()
{
return m_used;
}


int CShortBag::occurances( short element )
{
int occurs = 0;
for (int i = 0; i < m_used; i++ )
{
if ( m_data[i] == element )
occurs++;
}

return occurs;
}


void CShortBag::insert( short element )
{
if ( m_used < m_capacity )
{
m_data[ m_used ] = element;
m_used++;
}
}


void CShortBag::remove( short element )
{
if ( m_used > 0 )
{
for ( int i = 0; i < m_used; i++ )
{
if ( m_data[i] == element )
{
m_data[i] = m_data[--m_used];
break;
}
}
}
}
//end of program shortbag.cpp

Greg Comeau

unread,
Oct 7, 1999, 3:00:00 AM10/7/99
to
In article <37fd1394$0$10...@news.voyager.net> hur...@nospam.anti-social.com (Hurley) writes:
>I get a compiler error in Microsoft Visual C++ 5.0 when I try to use the line
>
>static const int size = 10; // or similar expression

Apparently it does not suppport "member constants", and probably
thinks it is a pure virtual specification (= 0) gone awry.

Make sure it knows you're trying to shoot for strict Standard C++,
perhaps you will need to set certain options in order to accomplish this
(however, you may run into other problem doing so though :{ ).

>this is code the instructor handed out in class, so changing the code is not
>an option).

It has to be if you don't want to change compilers.
You can do this instead:

enum { size = 10 };

- Greg
--
Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214
Producers of Comeau C/C++ 4.2.38 -- NOTE 4.2.42 BETAS NOW AVAILABLE
Email: com...@comeaucomputing.com / Voice:718-945-0009 / Fax:718-441-2310
*** WEB: http://www.comeaucomputing.com ***

Anthony Borla

unread,
Oct 8, 1999, 3:00:00 AM10/8/99
to

Hurley wrote in message <37fd1394$0$10...@news.voyager.net>...

>I get a compiler error in Microsoft Visual C++ 5.0 when I try to use the
line
>

<SNIPPED>

Someone from news:microsoft.public.vc.language or news:microsoft.public.vc
may have an answer for you.

I hope this helps.


Hurley

unread,
Oct 8, 1999, 3:00:00 AM10/8/99
to
Thanks for the tips everyone. The "enum" suggestion will work great if I ever
have to write a class similar to the one my instructor gave in class. I
posted the error to a microsoft group as well to see if they can help with
compiler settings.

Thanks again,
-Hurley

In article <37fd1394$0$10...@news.voyager.net>, hur...@nospam.anti-social.com

(Hurley) wrote:
>I get a compiler error in Microsoft Visual C++ 5.0 when I try to use the line
>

>static const int size = 10; // or similar expression
>

>This code compiles just fine with other compilers. I documented the exact
>error code in the small bag class below. For some reason the compiler thinks
>I'm using a virtual function instead of a constant integer. I'm the only one
>in my class using VC++ 5.0, so my instructor could not help me. I really
>don't want to buy a new compiler. There must be a way to fix this. (Note:

>this is code the instructor handed out in class, so changing the code is not
>an option).
>

0 new messages