473,322 Members | 1,778 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,322 software developers and data experts.

Works in Visual C++ 2003 but not in 2005

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 ==========

Dec 20 '05 #1
6 1462
John wrote:
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
Dec 20 '05 #2
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
"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:%2***************@TK2MSFTNGP15.phx.gbl...
John wrote:
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

Dec 20 '05 #3
John wrote:
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

Dec 20 '05 #4
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

"John" <ni****@nomail.no> wrote in message
news:h8********************@rogers.com...
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
"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:%2***************@TK2MSFTNGP15.phx.gbl...
John wrote:
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


Dec 20 '05 #5
John wrote:
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
Dec 21 '05 #6
On Tue, 20 Dec 2005 18:26:30 -0800, "Carl Daniel [VC++ MVP]"
<cp*****************************@mvps.org.nospam > wrote:
John wrote:
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.


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.

--
Doug Harrison
Visual C++ MVP
Dec 21 '05 #7

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

Similar topics

3
by: Thom Little | last post by:
I have ASP.NET websites deployed on a number of servers. Thee were developed with Visual Studio 2003. Is it true that Visual Studio 2005 only supports .NET Framework 2.0 and I will be required...
3
by: Shapper | last post by:
Hello, I am starting 2 new projects to deliver in January 2006. I want to create them in Asp.Net 2.0 using Visual Studio 2005. All my clients web sites are Visual Studio 2003 projects in...
9
by: TD | last post by:
Which versions of Windows operating systems can you build applications for with Visual Studio 2003, in particular VB? How about Visual Studio 2005, in paricular VB? Thanks, TD
4
by: Skc | last post by:
We have a developer who has made an application in Visual Studio 2003 and this will not work in our version of Visual Studio 2002. Error message: Solution file loading error: The selected file...
3
by: robin9876 | last post by:
Is it possible to install Visual Studio 2003 and 2005 on the same pc?
5
by: Nathan Sokalski | last post by:
I recently upgraded from Visual Studio .NET 2003 to Visual Studio .NET 2005. Visual Studio .NET 2005 does not create the Global.asax files that Visual Studio .NET 2003 did, which I used for...
12
by: Nathan Sokalski | last post by:
I recently upgraded to from Visual Studio .NET 2003 to Visual Studio .NET 2005. In Visual Studio .NET 2003 when I would select 'Build' it would add a *.dll with the name of the Project to a /bin/...
6
by: Carol | last post by:
Hi. When I try to run Visual Studio 2003 I get the message "MS development environment is not installed for the current user. Please run setup to install the application." Is there any way to...
3
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, I must be out of my mind. I used visual studio .net 2003 before and I installed 2005 a while back, but seldom to use it. Recently I was required to start to learn C# asp.net. So I tried...
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
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
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: 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)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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.