Works in Visual C++ 2003 but not in 2005

J

John

Hi,

I am working on a C++ project which has been developed with Visual
Studio.Net 2003. When I compile it with Visual Studio 2005, it gives hundred
of errors. Two of the strange errors are following, which repeated in many
places:

1-error C2664: 'action' : cannot convert parameter 1 from 'Foo *' to 'const
Foo *&'

2-error C2248: 'std::vector<_Ty>::_Myfirst' : cannot access protected member
declared in class 'std::vector<_Ty>'
with
[
_Ty=oid
]
------------------

Borland C++Builder compiles the same code without any error! What's wrong
with Visual Studio 2005?
Does any body know what's the problem and how to fix it? I'd appreciate any
help.

Here is a simple test program to show the error:

// -- CPPConstTest1.cpp

class Foo{
public:
Foo();
private:
int index;
};

void action(const Foo*& cptr);

int main()
{

Foo* fPtr = new Foo();
action(fPtr);

return 0;
} // end of main
//---------------------------
Foo::Foo(){
index=0;
}
//----------------
void action(const Foo*& cptr){

}
//---------------------- Build Output with Visual Studio
2005: ---------------------
------ Build started: Project: CPPTestVS05, Configuration: Debug
Win32 ------
Compiling...
CPPConstTest1.cpp
..\CPPConstTest1.cpp(17) : error C2664: 'action' : cannot convert parameter 1
from 'Foo *' to 'const Foo *&'
Conversion loses qualifiers
CPPTestVS05 - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
 
C

Carl Daniel [VC++ MVP]

John said:
Hi,

I am working on a C++ project which has been developed with Visual
Studio.Net 2003. When I compile it with Visual Studio 2005, it gives
hundred of errors. Two of the strange errors are following, which
repeated in many places:

1-error C2664: 'action' : cannot convert parameter 1 from 'Foo *' to
'const Foo *&'

Comeau C++ agrees with VC++ 2005 - the code is ill-formed according to the
C++ standard.

Possible workarounds, depending on what you're doing in "action":

void action(const Foo* const & cptr);

or

void action(Foo * const & cptr);

or

void action(Foo *& cptr);

or

Foo* fPtr = new Foo();
action(const_cast<const Foo*&>(fPtr));

-cd
 
J

John

Thanks for your explanation. Do you men that VS2005 is more compliant with
ANSI Standard C++? I compiled it with Borland C++ Builder X, it complied
without error.

John
 
C

Carl Daniel [VC++ MVP]

John said:
Thanks for your explanation. Do you men that VS2005 is more compliant
with ANSI Standard C++? I compiled it with Borland C++ Builder X, it
complied without error.

Yes.

Your best bet for assessing C++ compliance is usually to go to the Comeau
online test drive:

http://www.comeaucomputing.com/tryitout/

generally speaking, if Comeau compiles it, it's legal, and if Comeau doesn't
compile it, it's not. There have been a few exceptions, but they're few and
far between.

-cd
 
J

John

Thanks Carl for your comments. As far as I now it's legal to pass a
non-const object as an argument to a function with const argument. I am
still not sure what's wrong with my code.
The action() doesn't change the content of its object argument, it just
reads the content of the objects. Any comment?

Thanks again.

John
 
C

Carl Daniel [VC++ MVP]

John said:
Thanks Carl for your comments. As far as I now it's legal to pass a
non-const object as an argument to a function with const argument. I
am still not sure what's wrong with my code.
The action() doesn't change the content of its object argument, it
just reads the content of the objects. Any comment?

Honestly, I'm not sure why this particular case is illegal either. Perhaps
someone who's spent more time thinking about const correctness can give an
argument why this case should be illegal - but since current versions of
Comeau and VC both agree that it's illegal, I stronly suspect that there is
indeed a rationale - obscure though it may be.

-cd
 
D

Doug Harrison [MVP]

Honestly, I'm not sure why this particular case is illegal either. Perhaps
someone who's spent more time thinking about const correctness can give an
argument why this case should be illegal - but since current versions of
Comeau and VC both agree that it's illegal, I stronly suspect that there is
indeed a rationale - obscure though it may be.

You guys are talking about the illegality of this, right?

void action(const Foo*& cptr);

Foo* fPtr;
action(fPtr);

The problem will become evident if you write it like this:

const int X = 0;

void f(const int*& p)
{
p = &X; // Fine
}

void g()
{
int* p;
f(p); // Illegal, which prevents the following oops
*p = 2; // Oops - would write to a const int
}

This is more commonly seen in its pointer guise:

void action(const Foo** cptr);

Foo* fPtr;
action(&fPtr); // Illegal

It's really the same thing.
 

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

Top