Compile error C4430 with Visual C++ 2005

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi,
I am working on a legacy code. Following is a line of the code which
complies with Visual C++.NET 2003 but not with Visual C++ 2005:

for ( int i = 0; i < (const)(X.size()); i++ ) {}// X is an object such as a
standard Vector

Compile error with Visual C++ 2005:
error C4430: missing type specifier - int assumed. Note: C++ does not
support default-int

Compiler expect (const int) not (const). My question is why do we need
(const) in the for loop? I am not sure of its purpose. Any explanation of
the problem and possible solutions?

Thanks for your help.

John
 
John said:
Hi,
I am working on a legacy code. Following is a line of the code which
complies with Visual C++.NET 2003 but not with Visual C++ 2005:

for ( int i = 0; i < (const)(X.size()); i++ ) {}// X is an object
such as a standard Vector

Compile error with Visual C++ 2005:
error C4430: missing type specifier - int assumed. Note: C++ does not
support default-int

Compiler expect (const int) not (const). My question is why do we need
(const) in the for loop? I am not sure of its purpose. Any
explanation of the problem and possible solutions?

(const) is both meanningless and illegal - take it out, or change it to
(const int), which would be legal but pointless.

-cd
 
John said:
Hi,
I am working on a legacy code. Following is a line of the code which
complies with Visual C++.NET 2003 but not with Visual C++ 2005:

for ( int i = 0; i < (const)(X.size()); i++ ) {}// X is an object such as a
standard Vector

Compile error with Visual C++ 2005:
error C4430: missing type specifier - int assumed. Note: C++ does not
support default-int

Compiler expect (const int) not (const). My question is why do we need
(const) in the for loop? I am not sure of its purpose. Any explanation of
the problem and possible solutions?

Thanks for your help.

John
John:

It is possible that (const) could implicitly mean (const int), but IMHO
"implicit int" was always one of the craziest things in C, and it has (I
think) long been deprecated in C++. Maybe now it is illegal. Get rid of it!

The real problem here is that std::vector::size() is a size_t, which is
unsigned (and 64 bits on 64-bit systems). Therefore to compile without
warnings you need the cast:

for ( int i = 0; i < (int)X.size(); i++ )

But really better is

for ( size_t i = 0; i < X.size(); i++ )

HTH,

David Wilkinson
 
The original source of "(const)" was probably over-zealous const-correctness.

You are correct, for(int i... doesn't make much sense (but not uncommon)
when you're iterating unsigned values. I would agree that using
for(size_t... is better than the original.

http://www.peterRitchie.com/
 
Back
Top