473,624 Members | 2,515 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VC++: Cast operator not used?

I'm porting some code to Visual C++ and have run into a problem - the
compiler won't use a user-written cast operator.

The code uses an envelope-letter approach to passing (potentially)
large pieces of data around, and requires that certain methods return
an Envelope with a specific kind of Letter as it's content. I have a
cast operator that converts from what I've got to what should be
returned, but it seems that the compiler only looks at constructors
for the return type.

The code works fine with the SGI MIPSPRO and Linux GCC compilers.
With MS Visual C++ .NET (v. 7.1.3008) it says:

CDP3x3LinearTra nsform.c++(196) : error C2668: 'DataPacket::Da taPacket' :
ambiguous call to overloaded function
DataPacket.h(53 ): could be 'DataPacket::Da taPacket(Letter &)'
DataPacket.h(52 ): or 'DataPacket::Da taPacket(const DataPacket &)'
DataPacket.h(51 ): or 'DataPacket::Da taPacket(const Envelope<T> &)'
where the offending line in CDP3x3LinearTra nsform.c++ looks like this:

DataPacket
CDP3x3LinearTra nsform::Output( ) const {
ChannelDataPack et result;
//... compute result here...
return result; // line 196
}

In this case I've got a ChannelDataPack et and need to return a
DataPacket. ChannelDataPack et is NOT derived from DataPacket (even
tho' you'd expect that, given their names), but they ARE related.
ChannelDataPack et DOES have a conversion operator to convert itself to
a DataPacket, however (see below). I've found two work-arounds:

#1: Call the cast operator explicitly:

return result.operator omiDataPacket() ;

#2: Explicitly cause the cast operator to be called by adding code:

DataPacket dp;
dp = result;
return dp;

I have two objections to both of these workarounds: a) If it won't
work as written, what's the point in having a cast operator? and b)
This is done in about 150 different places in 70 different classes.
That's just way too much typing for someone as lazy as I am.
Questions:

- Is this a known problem with VC++? I've looked in the MS knowledge
base and found nothing, but I might be looking for the wrong thing.

- Is there some way of getting the VC++ compiler to notice (and use)
the cast operator without having to inflict a MS-specific kludge all
over the code?
Thanks!
Code:

//---------------------------------------------
// The Envelope to be wrapped around a Letter.
//---------------------------------------------
class EnvelopeBase
{
public:
virtual ~EnvelopeBase() ;
EnvelopeBase();
EnvelopeBase(Le tter::WriteAndC opyModeType wcm);
EnvelopeBase(co nst EnvelopeBase& from);
EnvelopeBase& operator=(const EnvelopeBase& rhs);
};
template < class T >
class Envelope : public EnvelopeBase
{
public:
virtual ~Envelope();
Envelope();
Envelope( const Envelope& );
Envelope( Letter& letter );

Envelope& operator=( const Envelope& );
T* operator->();
const T* operator->() const;
};

//---------------------------------------------
// Letter - What gets put inside an Envelope.
//---------------------------------------------
class Letter
{
public:
enum WriteAndCopyMod eType { ValueImmediate, ValueDelayed,
Pointer };
enum ExemplarType { exemplar };
virtual ~Letter();
Letter();
Letter( const String& );
Letter( const Letter& );

protected:
friend class Envelope< Letter >;
};
//---------------------------------------------
// DataPacketLette r - the data we're interested in.
//---------------------------------------------
class DataPacketLette r : public Letter
{
public:
virtual ~DataPacketLett er();
DataPacketLette r();
DataPacketLette r( const DataPacketLette r& );
DataPacketLette r( ExemplarType );
DataPacketLette r( const String& );

protected:
DataPacketLette r( const String&, ExemplarType );
};
//---------------------------------------------
// DataPacket - the data we're interested in, wrapped in an Envelope.
//---------------------------------------------
class DataPacket : public Envelope< DataPacketLette r >
{
public:
virtual ~DataPacket();
DataPacket();
DataPacket( const Envelope< DataPacketLette r >& from );
DataPacket( const DataPacket& from );
DataPacket( Letter& letter );
DataPacket& operator=( const DataPacket& rhs );
operator DataPacketLette r&() const;
};


//---------------------------------------------
// ChannelDataPack et(Letter) - the data we're interested in, wrapped &
unwrapped.
//---------------------------------------------
class ChannelDataPack etLetter : public DataPacketLette r
{
public:
virtual ~ChannelDataPac ketLetter();
ChannelDataPack etLetter();
ChannelDataPack etLetter( int theInitialSize );
ChannelDataPack etLetter( const ChannelDataPack etLetter& );
ChannelDataPack etLetter( ExemplarType );
ChannelDataPack etLetter( const String& );
};
class ChannelDataPack et : public Envelope< ChannelDataPack etLetter >
{
public:
virtual ~ChannelDataPac ket();
ChannelDataPack et();
ChannelDataPack et( float theValue );
ChannelDataPack et( const ChannelDataPack et& theOriginal );
ChannelDataPack et( Letter& letter );
ChannelDataPack et& operator=( const ChannelDataPack et& theRhs );
ChannelDataPack et& operator=( const float& theRhs );
operator ChannelDataPack etLetter&() const;
operator DataPacket() const;
operator float() const;
};

Jul 23 '05 #1
0 1671

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

Similar topics

1
3615
by: Count Dracula | last post by:
I narrowed down the source of the error. The stand-alone program listed below reproduces it. The compilation finishes but the warning is too serious to ignore: cl -GX prog.cpp Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8168 for 80x86 Copyright (C) Microsoft Corp 1984-1998. All rights reserved.
5
2047
by: Felix I. Wyss | last post by:
Good Afternoon, I recently noticed that some very simple methods of a template declared and used in a DLL library get inlined when used by the DLL itself, but not by other DLLs and EXEs. After some investigating, I narrowed this down to a very odd behavior (bug?) of the VC++.NET 2003 compiler: If a class that is declared as __declspec(dllimport) derives from a template, that template's methods are never inlined, even if declared with...
4
2385
by: danip | last post by:
Hi, I used to have the following: class CArchive { public: // create and specify the filename to use CArchive(CString filename, DWORD size); virtual ~CArchive(); CArchive& operator<<(bool b);
9
4645
by: Stuart Carnie | last post by:
Due to the tightening of the VC++ compiler in 2005, I have run into a compiler error (from code that previously worked in 2003) using a CComPtr<ITypeLib> as an element of a std::list, as follows std::list<CComPtr<ITypeLib> >. I understand the problem, so am looking for the correct solution to my problem. On the line that attempts to call push_back, I receive "error C2664: 'std::allocator<_Ty>::construct' : cannot convert parameter 1...
5
2358
by: Frederick Gotham | last post by:
Before I begin, here's a list of assumptions for this particular example: (1) unsigned int has no padding bits, and therefore no invalid bit- patterns or trap representations. (2) All types have the same alignment requirements. (3) sizeof(double) >= sizeof(unsigned) ===================================== We can cast from double to unsigned int as follows:
4
6243
by: nmrcarl | last post by:
I'm trying to upgrade a large project from VS 6.0 to VS 2005. After fixing a lot of things that changed (mostly sloppy coding in the original project that VS2005 didn't allow), I got the release version to build successfully. Unfortunately, it crashes right away when I start it. So now I need to build the debug version. Unfortunately, the compiler gives innumerable error messages of the sort shown below. The #include file referenced...
3
3748
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and thawed it out. I built a console app using Microsoft Visual C++ 6 (VC++) and it worked great. Only one line in the header file had to be commented out. I built a console app using Borland C++ Builder 5. The linker complained of references to...
7
8441
by: Norman Diamond | last post by:
A project depends on VC runtime from Visual Studio 2005 SP1, and DotNet Framework 2. Options are set in the setup project properties, so if these two dependencies are not already installed then this installer will install them. But what about the situation where VC runtime has already been installed? In fact it's been installed twice. Although the project was built on a Windows XP system with Visual Studio 2005 SP1 and the results were...
7
3943
by: * Tong * | last post by:
Hi, I couldn't figure out how to properly type cast in this case: $ cat -n type_cast.c 1 #include <stdio.h> 2 3 typedef unsigned char Byte; 4 typedef signed char Small_Int; 5
0
8168
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8672
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8614
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8330
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7153
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4075
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4167
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2603
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1474
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.